Not logged in - Login
< back

Serializing an object to an XML file

The following illustrates how to serialize a data structure to an XML file. (Save to an XML file.)

// get the file name.
string exportFile = "c:\somefile.xml";

// declare a settings object and set the values.
System.Xml.XmlWriterSettings xmlSettings = new System.Xml.XmlWriterSettings();
// set the encoding
xmlSettings.Encoding = Encoding.UTF8;
// set the indenting to make the results look pretty (optional)
xmlSettings.Indent = true;

// create an XML Writer object.
System.Xml.XmlWriter xwr = System.Xml.XmlWriter.Create(exportFile, xmlSettings);

// create an instance of the XML Serializer object.
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.m_objOrder.GetType());

// serialize the data to our file.
x.Serialize(xwr, this.m_objOrder);

// close the XML Writer object.
xwr.Close();