Color Of Code

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

C# XML Serialization

E-mail Print

Assembly (reference)

System.Xml

Using directive

using System.Xml.Serialization;

Code: Creation of the serializer

The following code generates a serialization assembly on the fly (on first execution). This assembly is then used to read and write objects of the specified type.

XmlSerializer s = new XmlSerializer( typeof( MyType ) );

Code: Serialization

using (TextWriter writer = new StreamWriter( "data.xml" ))
{
s.Serialize( writer, myObject );
}

Code: Deserialization

using (TextReader reader = new StreamReader( "data.xml" ))
{
myObject = (MyType)s.Deserialize( reader );
}
Last Updated on Sunday, 05 September 2010 16:31
You are here Code Snippets C# XML Serialization