Reputation: 2093
I'm trying to do something that I can't really find any help on. I have a class, within this class it represents 2 other classes exposed as properties. I want the properties of the classes to sit under the root of the containing class, instead of having the class names and then values:
Example:
public Origin Origin { get; set; }
public Destination Destination { get; set; }
Both these define nodes that should be tagged in the XML as:
<RootClass>
<ValueFromOrigin />
<ValueFromDestination />
</RootClass>
Current, my serialization brings back:
<RootClass>
<Origin>
<ValueFromOrigin />
</Origin>
<Destination>
<ValueFromDestination />
</Destination />
</RootClass>
I have tried adding [XmlElement("RootClass")]
to the properties for both Origin
and Destination
but get an error, assuming that it's trying to duplicate the node for some reason. Has anyone tried this successfully?
Thanks for the help guys.
Eric
Upvotes: 1
Views: 295
Reputation: 11820
I think the simplest way if you have xml sample and dont know how to write class, is to use xsd.exe tool to generate classes.
Its very helpfull when you have big xml schema and dont want to spend many time to write classes for serialization.
Upvotes: 1
Reputation: 13638
You need custom serialization. Without tag information the default serializer wouldn't know how to deserialize all of your data.
Imagine if both those properties were Strings. Which Element goes with which property?
Upvotes: 4