[.net] Deserializing XML question

Started by
1 comment, last by hplus0603 18 years ago
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?
I like the DARK layout!
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
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]
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.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement