Tip

XML in .NET: Object serialization

XML in .NET: Object serialization
by Dale Michalk

This tip is excerpted from a much longer article on InformIT. It discusses the relationship between Microsoft's .NET framework

Requires Free Membership to View

and XML.

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.

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. The Listing shows the code for our classes.

Listing: 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;
}

To read all of this tip, click over to InformIT. You have to register there, but the registration is free.

Did you like this tip? We would like to know. Email to sound off.

Related Book

.Net Framework Essentials
Authors: Hoang Lam and Thuan L. Thai
Online Price: $29.95
Publisher Name: O'Reilly & Associates
Date published: June 2001
Summary:
.NET Framework Essentials is an objective, concise, and technical overview of the new Microsoft .NET Framework for developing Web applications and services. Specifically written for intermediate to advanced VB, C/C++, Java, and Delphi developers, .NET Framework Essentials is also useful to system architects and leaders who are assessing tools for future projects. The authors devote special attention to the writing of .NET components plus web applications and services. This book takes on all of the most important .NET Framework topics -- from the underlying Common Language Runtime (CLR) to its specialized packages for ASP.Net, Web Forms, Windows Forms, XML and data access (ADO.Net).


This was first published in September 2001

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.