20.18. Serializing Objects Using the XmlSerializer
In addition to the SOAP and binary formatters, the System.Xml.dll assembly provides a third formatter, System.Xml.Serialization.XmlSerializer. You can use this formatter to persist the public state of a given object as pure XML, as opposed to XML data wrapped within a SOAP message. Working with this type is a bit different from working with the SoapFormatter or BinaryFormatter type. Consider the following code, which assumes you have imported the System.Xml.Serialization namespace:
static void SaveAsXmlFormat(object objGraph, string fileName) { // Save object to a file named CarData.xml in XML format. XmlSerializer xmlFormat = new XmlSerializer(typeof(JamesBondCar)); using(Stream fStream = new ...
Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.