• Advertisement
Sign in to follow this  

[.net] Deserializing XML question

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I've got some XML that looks something like this:
<blah>
   <something></something>
   <somethingelse></somethingelse>
   <something></something>
</blah>
The stuff inside <something> and <somethingelse> is pretty rigid, so something and somethingelse could be just classes. But obviously blah is an array of either something's and somethingelse's, both of which can occur any number of times in any order. Is there any way to deserialize this XML into an array of classes? Also, is there a way to somehow add comments to the XML generated when you serialize an object?

Share this post


Link to post
Share on other sites
Advertisement
You can easily autogenerate C# code to deserialize that XML format. Simply use the xsd.exe tool to generate an .xsd file, and subsequently, a C# class.

I don't know about the XML comments though, you might have to just bite the bullet and write your own serialize/deserialize code using XmlWriter or something

Share this post


Link to post
Share on other sites
If you know how to turn "<something>...</something>" into a CSomething instance, then de-serializing just becomes walking your <blah> and calling the de-serialization functionality, appending each object to some list/collection.

You can make this slightly more flexible by building a dictionary/hash table from XML tag string to de-serializer object factory, and instantiate one object factory per tag kind you want to de-serialize.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement