advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the Code for this Article
Partners & Affiliates
advertisement
advertisement
Average Rating: 4/5 | Rate this item | 1 user has rated this item.
 Print Print
 
Customizing XML Serialization in .NET 2.0 Using the IXmlSerializable Interface
In .NET 2.0, Microsoft has exposed the methods of the IXmlSerializable interface, giving you explicit control over the XML schema as well as fine-grained control of the XML created when serializing or deserializing your objects at runtime. 

advertisement
n .NET 2.0, Microsoft has exposed the methods of the IXmlSerializable interface, giving you far more control over the way your objects get serialized to XML than in previous versions. Implementing IXmlSerializable in your own classes gives you explicit control over the XML schema as well as fine-grained control of the XML when serializing or deserializing your objects at runtime.


One of the most obvious weaknesses with XML serialization in .NET Framework 1.x was that it lacked the ability to customize the serialization and deserialization process. In that version, the IXmlSerializable interface provided limited serialization support geared primarily toward serializing DataSet objects. But in the .NET Framework 2.0, Microsoft has made the IXmlSerializable interface methods available for public consumption—meaning that you can implement those methods to exercise a finer level of control over the XML serialization and deserialization process. This article explores the new serialization features and demonstrates how to leverage them in your applications.

In .NET Framework 1.x you could add attributes to types at design time and also override those attributes with new values at run time; however, the entire process was based on attributes—you never really had complete control over the serialization process. The IXmlSerializable interface methods that have been present in the .NET Framework since version 1.x. (but only for internal use), are now available for general use. They are:

  • ReadXml
  • WriteXml
  • GetSchema
Of the three methods above, the GetSchema() method is now obsolete and you should not use it for serialization purposes. Accordingly, the GetSchema() method should just throw a System.NotImplementedException. Instead, you need to implement a static method that generates the schema. You then pass this static method as an argument to the XmlSchemaProviderAttribute class constructor.

The WriteXml() method converts an object into its XML representation. You have to write sufficient information to the XmlWriter stream to allow the ReadXml() method to reconstitute your object. Note that a number of classes in the .NET Framework 2.0 class library implement the IXmlSerializable interface by default, so you can serialize and deserialize them easily.

How the IXmlSerializable Methods Work
To define your very own custom serialization and deserialization process you just need to follow a few steps.

  • Implement the static method that generates the schema for the type
  • Decorate the type with the new attribute named XmlSchemaProvider and specify the static method that generates and inserts the schema in the XmlSchemaSet.
  • Implement the ReadXml() and WriteXml() methods. The ReadXml() method controls reading the serialized format. You provide it with an XmlReader to read from the stream and populate the type. The WriteXml() method controls writing the object in serialized form. You pass it an XmlWriter to write the data to the stream.
The ReadXml() method generates an object from its XML representation. It must reconstitute your object using the information that was written before by the WriteXml() method. When you first call this method, the reader stream is positioned at exactly the point that the WriteXml() method began writing. Typically, this is immediately after the start tag that indicates the beginning of your serialized object. When this method returns, it must have consumed exactly those nodes that were written by the WriteXml method, so that reader stream is positioned where that method left it. That's it. There is actually one more official method called GetSchema() that you must implement in the IXmlSerializable interface, but it's there for legacy purposes only.

Implementing the IXmlSerializable Interface
Now that you've had the basic overview of the various IXmlSerializable interface methods, here's an example implementation. Listing 1 shows an Employee class that implements IXmlSerializable.

The Employee class definition is marked with the XmlSchemaProvider attribute.

   <XmlSchemaProvider ("CreateEmployeeSchema")> _

The XmlSchemaProviderAttribute class indicates the name of the method to call to get the schema added to the internal schema set. It takes an XmlSchemaSet and returns the XmlQualifiedName of the starting element.

  Next Page: Reading and Writing the XML


Page 1: IntroductionPage 2: Reading and Writing the XML
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES