I’m working on a set of classes that will be used to serialize to XML. The XML is not controlled by me and is organized rather well. Unfortunately, there are several sets of nested nodes, the purpose of some of them is just to hold a collection of their children. Based on my current knowledge of XML Serialization, those nodes require another class.
Is there a way to make a class serialize to a set of XML nodes instead of just one. Because I feel like I’m being as clear as mud, say we have the xml:
<root> <users> <user id=''> <firstname /> <lastname /> ... </user> <user id=''> <firstname /> <lastname /> ... </user> </users> <groups> <group id='' groupname=''> <userid /> <userid /> </group> <group id='' groupname=''> <userid /> <userid /> </group> </groups> </root>
Ideally, 3 classes would be best. A class root with collections of user and group objects. However, best I can figure is that I need a class for root, users, user, groups and group, where users and groups contain only collections of user and group respectively, and root contains a users, and groups object.
Anyone out there who knows better than me? (don’t lie, I know there are).
Are you not using the XmlSerializer? It’s pretty damn good and makes doing things like this real easy (I use it quite a lot!).
You can simply decorate your class properties with some attributes and the rest is all done for you..
Have you considered using XmlSerializer or is there a particular reason why not?
Heres a code snippet of all the work required to get the above to serialize (both ways):