Intro
 FAQ
 Features
 Limitations
 The basics

 XSD vs Dingo
 Class Diagram
 NUnit Tests
 Java code
 SourceForge

 Plugins
 Custom Builders
 Tutorial
 quicktips

 Samples
   conversion
   dashSchema
   contact
   foobar
   geneology

 Links
  NUnit

The Basics

For those totally new to C# and have no clue what XmlSerializer is. I hope this provides a basic introduction to how attributes work with XmlSerializer and why Dingo generates all that square bracket stuff. If you happen to have fallen asleep the last two decades, a popular trend is including application metadata with objects. In .NET, they are attribute objects. In Java, it's called annotation.

I can hear some people saying, "So what's the big freaking deal with metadata? It's not new and has been around for a while." Metadata has been around for quite a while and those who pay attention to history can trace the roots of attributes/annotations. Unlike normal object metadata like field type and object type definition, metadata tries to address the needs of application frameworks. Sorry for the buzzwords. Say you have a cool GUI framework for getting data and updating the data dynamically. In a general sense, that can be considered a framework. Let's say the framework uses event listeners and messaging. Usually, not all data is dynamic and some are "less" dynamic than others. By "less dynamic", I mean data that can be reloaded by user actions. Typically, it's data that doesn't change often and does have to be absolutely up to date.

In a complex GUI application, there's going to be many objects. There are numerous approaches to organizing the metadata so the framework can determine which objects need to register for events. Before Microsoft, IBM and Sun started to push attributes/annotations, developers would write their own metadata framework for mapping object metadata and loading it at runtime. Metadata makes this type of work less tedious and more enjoyable. In the case of classes generated by Dingo, the metadata is required by XmlSerializer. What the metadata does is give the serializer hints about which objects to instantiate and which fields or properties to ignore.

In theory and practice, having metadata support at the language level should be more efficient at runtime. Of course, it can be abused like anything else, so it's not a magic bullet. If we look at how AspectWerkz tackled the problem, it's obvious annotations/attributes aren't critical. It makes life easier, but by no means is it a hammer for every problem. In a recent interview with bill joy, he expressed mixed feelings about annotations. To learn more about how how AspectWerkz deals with application metadata, go to their website.

using System;
using System.Xml.Serialization;

namespace geneology
{
    this tell the serializer the namespace and whether or not the object is nullable
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="geneology")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="geneology",
      IsNullable=false)]
    public class geneology 
    {
        #region fields
        this tell the serializer person is an array
        [System.Xml.Serialization.XmlArrayItemAttribute("child")]
        public person[] person;
        #endregion

        public geneology()
        {
        }
        #region properties

        this tell the serializer "ignore this attribute" and don't serialize to XML
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public person[] Person
        {
            get { return person; }
            set { person = value; }
        }
        #endregion

        #region methods

        public person[] getPerson()
        {
            return person;
        }

        public void setPerson(person[] param)
        {
            person = param;
        }
        #endregion
    }
}
					

There's an easy way to see how metadata affects XmlSerializer, remove the metadata from "Person" property. What happens is XmlSerializer will duplicate the data when an object is serialized out to XML.

With XMLIgnoreAttribute
<?xml version="1.0" encoding="utf-8"?>
<geneology xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="geneology">
  <person>
    <child>
      <child>
        <child>
          <firstName>jane</firstName>
          <lastName>doe</lastName>
          <dateOfBirth>2004-09-09T07:14:14.0070528-05:00</dateOfBirth>
          <gender>female</gender>
        </child>
      </child>
      <firstName>john</firstName>
      <lastName>doe</lastName>
      <dateOfBirth>2004-09-09T07:14:14.0070528-05:00</dateOfBirth>
      <gender>male</gender>
    </child>
  </person>
</geneology>					

Without XMLIgnoreAttribute
<?xml version="1.0" encoding="utf-8"?>
<geneology xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="geneology">
  <person>
    <child>
      <child>
        <child>
          <firstName>jane</firstName>
          <lastName>doe</lastName>
          <dateOfBirth>2004-09-09T08:03:55.3740480-05:00</dateOfBirth>
          <gender>female</gender>
        </child>
      </child>
      <firstName>john</firstName>
      <lastName>doe</lastName>
      <dateOfBirth>2004-09-09T08:03:55.3740480-05:00</dateOfBirth>
      <gender>male</gender>
    </child>
  </person>
  <Person>
    <person>
      <child>
        <child>
          <firstName>jane</firstName>
          <lastName>doe</lastName>
          <dateOfBirth>2004-09-09T08:03:55.3740480-05:00</dateOfBirth>
          <gender>female</gender>
        </child>
      </child>
      <firstName>john</firstName>
      <lastName>doe</lastName>
      <dateOfBirth>2004-09-09T08:03:55.3740480-05:00</dateOfBirth>
      <gender>male</gender>
    </person>
  </Person>
</geneology>
					

As you can see from the XML output, without "XmlIgnoreAttribute", the serializer simply duplicates the same data. If we contrast this to XStream, annotations aren't necessary. I haven't read through all of XStream source yet, so I don't know exactly how Joe uses runtime reflection to determine which classes to instantiate. I hope this introduction provides a decent explanation of how XmlSerializer works and why Dingo generates it.

Updated 9-10-2004 Peter

SourceForge.net Logo
Copyright 2004 Peter Lin