Click here to Skip to main content
13,558,244 members
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 24 Feb 2009
Licenced MIT

An XML Serializable Base Class

, 25 Feb 2009
Rate this:
Please Sign up or sign in to vote.
XmlObject is the class to derive from when you need high-performant XML serializable objects.


It is getting more and more common to use Service Oriented Architecture for writing applications. In some cases, we even base our domain model on XML. From this will arise the need for .NET classes to represent such XML structures as strongly-typed objects.


The easiest, most common way to implement an XML serializable object, is through declarative, Reflection based serialization. Program elements such as classes and properties are decorated with attributes that declare how to serialize this program element as XML. XmlSerializer is then used to generate objects from XML, and serialize objects to XML.

The other approach is to implement the IXmlSerializable interface, and manually parse the XML using XmlReader. This gives better performance and more control than declarative serialization, but it is harder to implement.

I wrote this XmlObject class, and friends, to help implementing IXmlSerializable based serialization, with as little effort as possible.

When To Use

If you have had the need to generate code from XML Schema, maybe you have tried out xsd.exe, and are not happy with the results. You could then look into generating code yourself, if you need more control of serialization, or if performance is of importance to you.

The XmlObject class is good for serving as the base class for XML Schema generated code, or generally just to represent some XML structure as an object.

If you use WCF, XmlObject derived classes are perfect to use with service contracts. The serialization/deserialization process will be performed seamlessly.


If you have used the Simple API for XML (SAX) before, you know that this approach is easier to use than XmlReader, although XmlReader is more efficient. I have tried to combine these strategies.

The following classes are part of the implementation:


This class implements IXmlSerializable, for fast XML reading and writing. When you derive this class, this class will "drive" the XML parsing process, and it will completely handle the XML generation process. You can override methods like ReadAttribute, ReadElement, or ReadText to "listen" for the specific content you are interested in. You typically use strongly typed properties for specific properties in your objects. However, these will not use backing fields, but instead, work with the Attributes and Elements dictionaries.


This class is basically just a Dictionary<XmlQualififiedName, object>. The XmlObject has two properties of this type: Attributes and Elements. These properties can be used to access attributes and elements, respectively.


Enumerations are very common in XML and XML Schema. It is also very common to make these extensible, so that you can choose a value from a pre-defined set, but you may also specify any normalized string value that is not pre-defined in this enumeration. For instance, like this:

<xsd:union memberTypes="ObjectiveActionCodeEnumType xsd:normalizedString"/>

xsd.exe will automatically convert properties of that type into strings. However, XmlCodeContentType is a class for representing this type of strongly typed constructs.


A static utility class which will come in handy when parsing XML code lists to CLR enums.


If you need high-performant XML serializable objects in your solution, you might want to have a look. Feel free to give me some feedback or ask a question.

The attached source code contains a simple example, but let me know if you want to see something from the real world.


  • February 24, 2009 -
  • February 24, 2009 -
    • Fixed article incoherency/article improvement


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Software Developer (Senior)
Norway Norway
Software Engineer at FAST, a Microsoft Subsidiary, in Oslo, Norway. Background in C++/PASCAL. Specialized in .NET and XML.

You may also be interested in...

Comments and Discussions

GeneralA lot of info but for what Pin
Donsw21-Mar-09 15:35
memberDonsw21-Mar-09 15:35 
GeneralMy vote of 2 Pin
Adam L. Stevenson25-Feb-09 10:52
memberAdam L. Stevenson25-Feb-09 10:52 
GeneralRe: My vote of 2 Pin
Dennis JD Myren25-Feb-09 11:34
memberDennis JD Myren25-Feb-09 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 25 Feb 2009
Article Copyright 2009 by baretta2
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid