Newbie: How to serialize/deserialize with XML?

D

deko

I understand I can use System.Xml.Serialization.XmlSerializer to convert an
object to XML, but I'm confused on the mechanics of this.

I have a C# .NET 2.0 Windows Forms app that contains a whole bunch of
user-defined properties that need to be persisted to an XML file. Each set
of properties comprises a "Thing" object.

So let's say I have a class - "Things" - with a dozen or so public
properties. And I create 10 Things. My application has a form in which
users can change the properties of these Things using various controls. So
now the user has carefully adjusted all the properties of his Things, and
then he closes the form and exits the application.

The next time the app is opened, how do I deserialize the Things? Sure, I
can serialize the Things class by marking the class with the [Serializable]
attribute. But how/where do I persist the values for all the properties?
What good is serializing the class? Does that automatically persist the
properties?

And what if I want to instantiate one particular Thing - "MyThing" - how to
I lookup the properties for MyThing and instantiate that one object without
instantiating all Things?

Thanks in advance.
 
G

Guest

deko,
Serializing your classes, either to Xml or to a byte array with the
BinaryFormatter, returns the serialized object to you. At this point it is up
to you how you want to persist it. For xml, you are going to get an
XmlDocument which you can simply Save to the filesystem.
Peter
 
D

deko

Serializing your classes, either to Xml or to a byte array with the
BinaryFormatter, returns the serialized object to you. At this point it is
up
to you how you want to persist it. For xml, you are going to get an
XmlDocument which you can simply Save to the filesystem.

So it sounds like I really don't need to serialize any objects, but rather
just the properties - is this correct?

Then, I can instantiate the object however I want and use a method in the
constructor to retrieve the properties from an XmlDocument? Does this kind
of functionality exist in the FCL, or do I have to write this myself?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top