Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Deserializing XML question

Started by BradDaBug Apr 4, 2006 at 8:39 PM 1 replies 550+ views
Original Post
BradDaBug
BradDaBug
Say I've got some XML that looks something like this:
<blah>
   <something><!-- stuff here --></something>
   <somethingelse><!-- more stuff --></somethingelse>
   <something><!-- stuff again --></something>
</blah>
The stuff inside and 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!
joelmartinez
joelmartinez
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]
hplus0603
hplus0603
If you know how to turn "..." into a CSomething instance, then de-serializing just becomes walking your 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 };

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.