views:

297

answers:

5

Hello, all. I seem to forgot the process and how to set a class property to xml element or attribute by setting attribute on the property member. So you can write out the object to xml.

I'm not sure I'm making any sense here but hopefully someone know what I'm talking about and point me to a how-to or tutorial or MSDN document.

Thank

+2  A: 

Property needs to be public, and just add the XmlElementAttribute/XmlPropertyAttribute as you wish.

Rowland Shaw
+1  A: 

Look into the XmlSerializer class.

There are a lot of different options on how to control the xml that is generated. For example, you could start with a schema (xsd) and generate your classes from that then use the XmlSerializer to convert the class to xml.

Or, provided the classes already exist, just add the syntactic sugar necessary to emit the xml you want.

One decent article is at: http://www.dotnetjohn.com/articles.aspx?articleid=173

Chris Lively
A: 
[System.Xml.Serialization.XmlElement]
public string MyHappyThing { get; set; }

It is probably recommended you apply some further information to the attribute, you can read up about it here.

Quintin Robinson
A: 

You want the System.Runtime.Serialization namespace. Use DataContract and DataMember for your attributes, and XmlObjectSerializer for your serialization.

Randolpho
A: 

System.Xml.Serialization.XmlRootAttribute System.Xml.Serialization.XmlRoot XmlElementAttribute XmlArray

Those are some of the key items to look for.

Full example available at: http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

....