In addition to the Serialization namespace, which contains the SOAP and BinaryFormatter classes for serializing data into SOAP and binary format, the .NET Framework provides another name space for serializing data: the XmlSerialization namespace. As you can guess, the XmlSerialization namespace provides methods for serializing and deserializing objects in XML format. XML serialization differs from the other two serialization forms in that it serializes only public properties and fields; read-only and private properties are not serialized. Therefore, XML serialization doesn’t preserve the state of the object being serialized. The output of XML serialization is both human and machine readable and doesn’t require that classes be marked with the attribute. Moreover, you have control over the schema of the XML document that’s produced with the help of attributes.
As you already know, XML is a standard protocol for transferring data between computers and operating systems. The result of XML serialization is an XML document, which can be used outside the context of the specific application or even the Framework itself. In other words, you don’t need the classes that describe the objects being serialized to use the XML document. To use XML serialization, you must create an instance of the XmlSerializer class and then call its Serialize method (or the Deserialize method to extract data from an XML stream and populate an instance of a custom class). There’s a major difference, however. The XmlSerializer class must be told in its constructor the type of object it’s going to serialize. The constructor of the XmlSerializer class requires an argument, which is the type of objects it will serialize or deserialize. Here’s how to set up a new instance of the XmlSerializer class:
Imports System.Xml.Serialization Dim serializer As New XmlSerializer(CO.GetType) Dim FS As FileStream FS = New FileStream(path, FileMode.Create) serializer.Serialize(FS, CO) FS.Close()Code language: PHP (php)
The first statement imports the System.Xml.Serialization namespace so that we won’t have to fully qualify our references to the members of this class. The CO variable is an instance of the custom class, whose instances we intend to serialize through the XmlSerializer class. You can also pass the name of the class itself to the constructor by using a statement such as the following:
Dim serializer As New XmlSerializer(GetType(CustomClass))Code language: PHP (php)
The serializer object can be used to serialize only instances of the specific class, and it will throw an exception if you attempt to deserialize a different class with it. Note also that all classes are XML-serializable by default, and you don’t have to prefix them with the attribute.
XmlSerializer can’t serialize arbitrary objects. You must tell the XmlSerializer class the type of object it will serialize. In the background, CLR will create a temporary assembly, a process that will take a few moments. The temporary assembly, however, will remain in memory as long as the application is running. After the initial delay, XML serialization will be quite fast.