By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

How to convert C# class to XML ?

P: n/a
I'm building my first major C# WinForms application. I've successfully
completed the forms and the data model and they work great. Now I'm moving
on to saving/opening the data model to/from the hard drive.

In my VB6 past I've almost exclusively used MDB files for this purpose but
I'm thinking that XML would be a simpler, better solution for this
application. So I tried using "XMLSerialization" but it failed. I'm not
really sure why but am guessing because my data model doesn't comply with all
the restrictions it imposes.

So today I've been reading about standard XML formats. It all seems
straightforward and I'm ready to start coding. But I'm wondering if there is
a tool within or without of Visual Studio that will simply take my data model
(a complex class) and produce the XML data and/or an XML schema?

Any other advice or articles on this would be appreciated too.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
For the most part, automatic XML serialization is a simple matter, provided
you follow the rules which are not the most well known. You can always
manually create an XML document based on the values of your class... but that
would be a little work, a little more work would be building your own
serialization system using reflection, but that would take a little more work.

I would suggest going over your class and verify that all properties you
want to serialize have the [XmlElement()] attribute with a good name, and
that all properties you do not want used have the [XmlIgnore()] attribute.

Another thing to watch for is complicated objects that cannot easily be
represented as a string... a raw object for instance tends not to be able to
be serialized. Other items such as TimeSpans cannot be automatically

Worst case, set all but one of your properties to [XmlIgnore()] and do some
testing, see if the class will serialize with it, if so, convert one of the
new [XmlIgnore()]’s back to an element and try again. Keep doing this until
you identify which property it is that is keeping you from easily serializing.

Once that’s all done, deserializing is just as easy, just make sure though
that all of your properties marked with [XmlElement()] have sets attached and
you have a default constructor for the class.

Brendan

"Robert W." wrote:
I'm building my first major C# WinForms application. I've successfully
completed the forms and the data model and they work great. Now I'm moving
on to saving/opening the data model to/from the hard drive.

In my VB6 past I've almost exclusively used MDB files for this purpose but
I'm thinking that XML would be a simpler, better solution for this
application. So I tried using "XMLSerialization" but it failed. I'm not
really sure why but am guessing because my data model doesn't comply with all
the restrictions it imposes.

So today I've been reading about standard XML formats. It all seems
straightforward and I'm ready to start coding. But I'm wondering if there is
a tool within or without of Visual Studio that will simply take my data model
(a complex class) and produce the XML data and/or an XML schema?

Any other advice or articles on this would be appreciated too.

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.