views:

451

answers:

3

I've done some XML serialization before but i used Attributes, I'm not sure this is doable for my next assignment, here's a brief list of XML manip requirementes.

  • General Purpose XMl manipulation, tied to a treeview, no schema.
  • Load/Save XML.
  • Load/Save Attributes as well as Values (i believe the term is Element Text?), and be mindful of the Node's name.
  • Comments can be safely ignored as can Document info markup (ie, the UTF-8 and schema tags)

Any suggestions on how best to handle this?

A: 

I probably wouldn't bother with an object model and IXmlSerializable - it sounds like you might just as well talk in terms of an XmlElement / XmlDocument - i.e. pass the data around as a block of xml. Since you have no schema it would be pointless to shred it out; you might as well do it via an xml DOM.

When you say treeview - is this winforms, asp.net, wpf? I believe the asp.net treeview can take an xml source, but for winforms you'd have to iterate the nodes yourself.

Marc Gravell
unfortunately it's winforms, i'm much more comfortable with WPF tho.
Firoso
A: 

I would suggest you to use the simple XML serialization supported by the .NET framework.

Go through these MSDN documentation

How to Serialize an object

How to Deserialize an object

Pradeepneo
Can you say why XML Serialization would be better for his needs? What about maintainability?
John Saunders
could you tell me what's the problem with XMLSerializer?. why do you think maintainability is a problem?
Pradeepneo
Would the down voter care to explain?. I guess you need to down vote only when there's something wrong. not because you don't like it.
Pradeepneo
A: 

Don't know what exactly you mean with "before but i used Attributes" but I would recommend XmlSerializer too:

  • With "simple" classes it works usually out of the box.
  • Collections might need some more work, but it depends on your requirements and object structure.
  • There are other build in XML serializers like XAML or the WCF DataContractSerializer. All have pros and cons. But if you want to fine tune your XML format, XMLSerializer is the most flexibel one.
  • You can approach your format step by step: If the default looks good, your done. If not you have to add just some attributes in most cases.
  • If you want complete control, you can still implement IXmlSerialize to fine tune your format.
  • Everything applies on a per class basis: Use the default where appropriate, add some attributes where required and implement IXmlSerializable as required.
Achim
Perhaps because of its association with ASMX web services, very few XML Serialization bugs will ever be fixed. That's a necessary consideration for a production application.
John Saunders