Home All Groups Group Topic Archive Search About

Controlling XML Structure with XMLSerialization

Author
2 Oct 2006 7:54 PM
zacks
When I figured out XMLSerialization, I thought that was the best thing
since sliced bread!

Since then, I have designed several XML files that I read and write via
XMLSerialzation. A common feature most of these have are repeating
nodes. I have figured out how to setup repeating nodes but I don't
like how the resulting XML looks. I have heard that it really
shouldn't matter how an XML file looks as long as it contains the
right data. But some of these may need to be occasionally manually
edited, and I just think they don't look right.

Example:

Public Class Child
Private _Prop1 as String
Private _Prop2 as String
Public Property Prop1 as String
    Get
        Return _Prop1
    End Get
    Set(ByVal value as String)
        _Prop1 = value
    End Set
End Property
Public Property Prop2 as String
    Get
        Return _Prop2
    End Get
    Set(ByVal value as String)
        _Prop2 = value
    End Set
End Property
End Class

Public Class Parent
Private _Children as List(of Child)
Public Property Children as List(of Child)
    Get
        Return _Children
    End Get
    Set(ByVal value as List(of Child)
        _Children = value
    End Set
End Propertyt
End Class

This produces an XML file that looks like (with two children):

<Parent>
    <Children>
        <Child>
            <Prop1 />
            <Prop2 />
        </Child>
        <Child>
            <Prop1 />
            <Prop2 />
        </Child>
    </Children>
</Parent>

I would prefer that the XML file look like this:

<Parent>
    <Child>
        <Prop1 />
        <Prop2 />
    </Child>
    <Child>
        <Prop1 />
        <Prop2 />
    </Child>
</Parent>

Any suggestions on how to structure the Classes to accomplish this?

Author
3 Oct 2006 6:16 AM
GhostInAK
Hello za***@construction-imaging.com,

The .NET Serializer actually produces correct XML.  Any repeating nodes *SHOULD*
be contained within a node that depicts plurality.

-Boo

Show quoteHide quote
> When I figured out XMLSerialization, I thought that was the best thing
> since sliced bread!
>
> Since then, I have designed several XML files that I read and write
> via
> XMLSerialzation. A common feature most of these have are repeating
> nodes. I have figured out how to setup repeating nodes but I don't
> like how the resulting XML looks. I have heard that it really
> shouldn't matter how an XML file looks as long as it contains the
> right data. But some of these may need to be occasionally manually
> edited, and I just think they don't look right.
> Example:
>
> Public Class Child
> Private _Prop1 as String
> Private _Prop2 as String
> Public Property Prop1 as String
> Get
> Return _Prop1
> End Get
> Set(ByVal value as String)
> _Prop1 = value
> End Set
> End Property
> Public Property Prop2 as String
> Get
> Return _Prop2
> End Get
> Set(ByVal value as String)
> _Prop2 = value
> End Set
> End Property
> End Class
>
> Public Class Parent
> Private _Children as List(of Child)
> Public Property Children as List(of Child)
> Get
> Return _Children
> End Get
> Set(ByVal value as List(of Child)
> _Children = value
> End Set
> End Propertyt
> End Class
>
> This produces an XML file that looks like (with two children):
>
> <Parent>
> <Children>
> <Child>
> <Prop1 />
> <Prop2 />
> </Child>
> <Child>
> <Prop1 />
> <Prop2 />
> </Child>
> </Children>
> </Parent>
> I would prefer that the XML file look like this:
>
> <Parent>
> <Child>
> <Prop1 />
> <Prop2 />
> </Child>
> <Child>
> <Prop1 />
> <Prop2 />
> </Child>
> </Parent>
> Any suggestions on how to structure the Classes to accomplish this?
>