1

Is there a native way to map the following xml...

<Tag type="1" />
<Tag type="2" />

... to the concrete classes "Type1Tag" and "Type2Tag" (both deriving from abstract class Tag), based on the value of the "type" attribute?

(something similar to NHibernate's discriminator: DiscriminateSubClassesOnColumn(...)/DiscriminatorValue(...) )

I'm not looking for something like the mapping reported in XmlSerializer + Polymorphism , which discriminates types by the tag name, instead of by an attribute value (I can't change the xml structure) :)

Community
  • 1
  • 1
Notoriousxl
  • 1,540
  • 1
  • 16
  • 27

1 Answers1

3

I'm probably too late in your implementation to be of any help, but this is something I figured out this weekend and thought I'd share for other people to find as it seems to meet my needs.

Please note that there is no error handling here, and you can create your own child object type initialization depending on your needs.

I have a number of different classes that are all of the same type of element, but could hold different content based on the value of an attribute.

I used the IXmlSerializable-interface and implemented the method to read each type tag...

Xml Input:

<Parent>
  <Tag type="1"/>
  <Tag type="2"/>
</Parent>

The parent class is what implements IXmlSerializable:

public class Parent : IXmlSerializable
{
    [XmlElement("Tag")]
    public List<TagBase> Tags { get; set; }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
       // this line causes the root Parent node to be ignored, and gets to the children Tag elements
       reader.Read();

       while (reader.ReadToNextSibling("Tag"))
       {
           string attributeType = reader.GetAttribute("type");

           Type type = null;

           switch(attributeType)
           {
              case "1":
                  type = typeof(Type1Tag);
                  break;
              case "2":
                  type = typeof(Type2Tag);
                  break;
              default:
                  continue;
           }

           XmlSerializer serializer = new XmlSerializer(type);

           // this line is the key to rejoining the automatic xml deserialization of all
           // the child stuff
           TagBase tagBase = (TagBase)serializer.Deserialize(reader.ReadSubtree());

           this.Tags.Add(tagBase);
        }
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        throw new NotImplementedException();
    }

    #endregion IXmlSerializable Members
}

I created an abstract TagBase-class that each tag type would inherit:

[XmlRoot("Tag")]
public abstract class TagBase
{
   [XmlAttribute("type")]
   public string Type { get; set; }
}

Then, each other class can be implemented normally with your custom properties, etc...

[XmlRoot("Tag")]
public class Type1Tag : TagBase
{
    // your implementation
}

[XmlRoot("Tag")]
public class Type2Tag : TagBase
{
    // your implementation
}

Note, that I used XmlRootAttribute here and included them, as I had a bunch of namespaces that caused exceptions when trying to deserialize the child tags, but YMMV. I also haven't gotten to the point of adding the WriteXml-method, but should be pretty straight forward here...

Spontifixus
  • 6,570
  • 9
  • 45
  • 63
Bryan
  • 88
  • 1
  • 8