Exam 70-553 - Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.

Section 1

  • Part 3
    • Topic 2

Control the serialization of an object into XML format by using the System.Xml.Serialization namespace.

  • Serialize and deserialize objects into XML format by using the XmlSerializer class.
  • Control serialization by using serialization attributes.
  • Implement XML Serialization interfaces to provide custom formatting for XML serialization.
  • Delegates and event handlers provided by the System.Xml.Serialization namespace

Summary

The XmlSerializer Class can be used to easily serialize and deserialize an object to Xml. Below are examples of Serializing and Deserializing an object using this class:

Serialization
MyTypeOfObject myObject = new MyTypeOfObject();
XmlSerializer mySerializer = new XmlSerializer(typeof(MyTypeOfObject));
XmlTextWriter myWriter = new XmlTextWriter("MyObject.xml", System.Text.Encoding.UTF8);
mySerializer.Serialize(writer, myObject);
myWriter.Close();

Deserialization
XmlSerializer mySerializer = new XmlSerializer(typeof(MyTypeOfObject));
XmlTextReader myReader = new XmlTextReader("MyObject.xml");
MyTypeOfObject myObject = (MyTypeOfObject) mySerializer.Deserialize(myReader);

To Control Serialization by using Serialization attributes simple apply the attributes described in the previous section to public properties and methods.

The System.Xml.Serialization Namespace provides the IXmlSerializable Interface to provide custom formatting for XML serialization. For a good explanation for why you would use this interface read the following two paragraphs taken from msdn2.

There are two reasons to implement this interface. The first is to control how your object is serialized or deserialized by the XmlSerializer. For example, you can chunk data into bytes instead of buffering large data sets, and also avoid the inflation that occurs when the data is encoded using Base64 encoding. To control the serialization, implement the ReadXml and WriteXml methods to control the XmlReader and XmlWriter classes used to read and write the XML.

The second reason is to be able to control the schema. To enable this, you must apply the XmlSchemaProviderAttribute to the serializable type, and specify the name of the static member that returns the schema. See the XmlSchemaProviderAttribute for an example.

The following list of delegates and event handler comes right from the first article in the resources

UnknownAttribute event - thrown when the serializer encounters an unknown attribute. By default the Xml serializer ignores unknown attributes.

UnknownElement event - thrown when the serializer encounters an unknown element. By default the Xml serializer ignores unknown elements.

UnknownNode event -thrown when the serializer encounters an unknown node. By default the Xml serializer ignores unknown node.

UnreferencedObject event - section 5 of the SOAP document at w3c. Basically you can reference other object within the same Xml document. This event is thrown when it can't find the referenced object.

Other Resources & Links:

XmlSerializer.Net Tutorial
http://www.topxml.com/xmlserializer/default.asp

IXmlSerializable Interface
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable(VS.80).aspx

What Did you learn Today (List of Xml Delegates/Events)
http://blog.denoncourtassociates.com/default,date,2006-02-01.aspx

Exam 70-553 - Access files and folders by using the File System classes.

The Tale of the Evil Laptop Breaking Skunk