C# Object Serialization Flavors: Binary, SOAP, XML
Listing 2 showed binary format serialization and de-serialization. The two other formats (SOAP and XML) are used to implement different serialization strategies. Listing 3 illustrates XML serialization.
Listing 3 XML serialization.
XmlSerializer xmlFormat = new XmlSerializer(typeof(WMI1)); fStream = new FileStream("WmiData.xml", FileMode.Create, FileAccess.Write, FileShare.None); xmlFormat.Serialize(fStream, netIfs); fStream.Close(); Console.WriteLine("-> Saved netIfs in XML format.");
As with binary format serialization, XML serialization is very simple. You create instances of XmlSerializer and FileStream; then you call xmlFormat.Serialize(), passing in the FileStream object and the object that you want to serialize. The end result should be an XML file that looks like Listing 4.
Listing 4 XML serialized data.
<?xml version="1.0"?> <WMI1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <ifDetails>Adapter name: Intel[R] PRO_100 VE Network Connection - Packet Scheduler Miniport Adapter name: MS TCP Loopback interface </ifDetails> <ipRouteDetails>Destination: 0.0.0.0, NextHop: 192.168.1.254 Destination: 127.0.0.0, NextHop: 127.0.0.1 Destination: 192.168.1.0, NextHop: 192.168.1.1 Destination: 192.168.1.1, NextHop: 127.0.0.1 Destination: 192.168.1.255, NextHop: 192.168.1.1 Destination: 224.0.0.0, NextHop: 192.168.1.1 Destination: 255.255.255.255, NextHop: 192.168.1.1 </ipRouteDetails> </WMI1>
When you execute the supplied code, the associated binary and XML format files are located in the folder bin\debug. The latter is located just under the folder that contains the SharpDevelop solution file. Notice how the data in Listing 4 matches the data displayed in the DOS console in Figure 1.
Figure 1 The running program: serialization and de-serialization in action.
Listing 5 shows the code to serialize in SOAP format.
Listing 5 C# code to drive the SOAP format serialization.
SoapFormatter soapFormat = new SoapFormatter(); fStream = new FileStream("WmiData.soap", FileMode.Create, FileAccess.Write, FileShare.None); soapFormat.Serialize(fStream, netIfs2); fStream.Close(); Console.WriteLine("Saved WMI1 object in SOAP format");
Very straightforward. Just one thing to note: To run the SOAP format serialization, you need to add an assembly reference:
System.Runtime.Serialization.Formatters.Soap
Listing 6 shows the serialized objects in SOAP format—the language of web services!
Listing 6 SOAP format serialization.
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:WMI1 id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MgmtWithWMI/MgmtWithWMI%2C%20Version%3D1.0.3023.21959%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <networkInterfaces id="ref-3">Adapter name: Intel[R] PRO_100 VE Network Connection - Packet Scheduler Miniport Adapter name: MS TCP Loopback interface </networkInterfaces> <ipRoutes id="ref-4">Destination: 0.0.0.0, NextHop: 192.168.1.254 Destination: 127.0.0.0, NextHop: 127.0.0.1 Destination: 192.168.1.0, NextHop: 192.168.1.1 Destination: 192.168.1.1, NextHop: 127.0.0.1 Destination: 192.168.1.255, NextHop: 192.168.1.1 Destination: 224.0.0.0, NextHop: 192.168.1.1 Destination: 255.255.255.255, NextHop: 192.168.1.1 </ipRoutes> </a1:WMI1> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
As you can see, it isn’t difficult to manage the serialization and de-serialization process. Just in case you’ve had any difficulty getting the code to work, it might be useful to take a look at the version of .NET deployed on your system. We’ll consider this process next.