XML in .NET: Object Serialization
- XmlSerializer
- Demonstrating Serialization
- Serialization Attributes
XML stands at the center of the .NET universe. Unlike previous development platforms such as Visual Basic or Java, in which XML parsers were bolted on as XML evolved, the .NET framework was designed to utilize and embrace XML technology at the core of the platform from the start. One of the more powerful facilities is the capability to store and retrieve object state from XML in a process called XML object serialization. This article shows how to use this capability to parse and create XML documents with ease and minimal coding.
The .NET framework provides high-fidelity type information about classes loaded into the Common Language Runtime. The runtime provides a fully populated System.Type object when invoking the GetType method of an object or the typeof operator with a class name. System.Type provides methods to query for information on the interfaces, methods, properties, events, and fields that a class implements and then dynamically invoke them to retrieve values. Using this information, we could easily build our own program to interpret between object data and XML documents. Fortunately, the class XmlSerializer exists in the framework and is ready and willing to do the job for us.
XmlSerializer
The XmlSerializer handles XML in a manner similar to the approach taken by most XML parsers that comply with DOM interfaces. Like a parser, it scans XML sources to build an object model that the client can eventually access. Instead of building up a model of DOM Infoset items, the XmlSerializer builds up the object structure that relates to the type information provided to it. It also goes in the opposite direction to enumerate the runtime characteristics of a class instance to create an XML document with elements and attributes set to the proper values.
To illustrate the serialization process, we will use an object model that represents a security domain similar to that provided by NT/Windows 2000. At the top is a domain object holding a collection of Users and Groups. The users hold authentication information such as usernames and passwords, while the groups hold references to users to organize them for easy access control. All objects in the model have ID information to identify them uniquely outside of their textual names, much like the use of SIDs in NT Security. A good application of this object model is an ASP.NET solution that requires a security database outside the normal NT/Windows 2000 domain infrastructure. Figure 1 shows the UML diagram and Listing 1 shows the code for our classes.
Figure 1 Domain classes UML diagram.
Listing 1: Code Listing for Domain Security Classes
public class Domain { public string ID; public string Name; public User[] Users; public Group[] Groups; } public class User { public string ID; public string Name; public string Password; } public class UserRef { public string ID; } public class Group { public string ID; public string Name; public UserRef[] Users; }