Controlling XML Structure with XMLSerialization

Z

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?
 
G

GhostInAK

Hello (e-mail address removed),

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

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top