Export (0) Print
Expand All

How to: Control Serialization of Derived Classes

Using the XmlElementAttribute attribute to change the name of an XML element is not the only way to customize object serialization. You can also customize the XML stream by deriving from an existing class and instructing the XmlSerializer instance how to serialize the new class.

For example, given a Book class, you can derive from it and create an ExpandedBook class that has a few more properties. However, you must instruct the XmlSerializer to accept the derived type when serializing or deserializing. This can be done by creating a XmlElementAttribute instance and setting its Type property to the derived class type. Add the XmlElementAttribute to a XmlAttributes instance. Then add the XmlAttributes to a XmlAttributeOverrides instance, specifying the type being overridden and the name of the member that accepts the derived class. This is shown in the following example.

Example

public class Orders
{
    public Book[] Books;
}    

public class Book
{
    public string ISBN;
}

public class ExpandedBook:Book
{
    public bool NewEdition;
}

public class Run
{
    public void SerializeObject(string filename)
    {
        // Each overridden field, property, or type requires 
        // an XmlAttributes instance.
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute instance to override the 
        // field that returns Book objects. The overridden field
        // returns Expanded objects instead.
        XmlElementAttribute attr = new XmlElementAttribute();
        attr.ElementName = "NewBook";
        attr.Type = typeof(ExpandedBook);

        // Adds the element to the collection of elements.
        attrs.XmlElements.Add(attr);

        // Creates the XmlAttributeOverrides instance.
        XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides();

        // Adds the type of the class that contains the overridden 
        // member, as well as the XmlAttributes instance to override it 
        // with, to the XmlAttributeOverrides.
        attrOverrides.Add(typeof(Orders), "Books", attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s = 
        new XmlSerializer(typeof(Orders), attrOverrides);

        // Writing the file requires a TextWriter instance.
        TextWriter writer = new StreamWriter(filename);

        // Creates the object to be serialized.
        Orders myOrders = new Orders();
        
        // Creates an object of the derived type.
        ExpandedBook b = new ExpandedBook();
        b.ISBN= "123456789";
        b.NewEdition = true;
        myOrders.Books = new ExpandedBook[]{b};

        // Serializes the object.
        s.Serialize(writer,myOrders);
        writer.Close();
    }

    public void DeserializeObject(string filename)
    {
        XmlAttributeOverrides attrOverrides = 
            new XmlAttributeOverrides();
        XmlAttributes attrs = new XmlAttributes();

        // Creates an XmlElementAttribute to override the 
        // field that returns Book objects. The overridden field
        // returns Expanded objects instead.
        XmlElementAttribute attr = new XmlElementAttribute();
        attr.ElementName = "NewBook";
        attr.Type = typeof(ExpandedBook);

        // Adds the XmlElementAttribute to the collection of objects.
        attrs.XmlElements.Add(attr);

        attrOverrides.Add(typeof(Orders), "Books", attrs);

        // Creates the XmlSerializer using the XmlAttributeOverrides.
        XmlSerializer s = 
        new XmlSerializer(typeof(Orders), attrOverrides);

        FileStream fs = new FileStream(filename, FileMode.Open);
        Orders myOrders = (Orders) s.Deserialize(fs);
        Console.WriteLine("ExpandedBook:");

        // The difference between deserializing the overridden 
        // XML document and serializing it is this: To read the derived 
        // object values, you must declare an object of the derived type 
        // and cast the returned object to it.
        ExpandedBook expanded;
        foreach(Book b in myOrders.Books) 
        {
            expanded = (ExpandedBook)b;
            Console.WriteLine(
            expanded.ISBN + "\n" + 
            expanded.NewEdition);
        }
    }
}

See Also



Build Date:

2012-08-02

Community Additions

ADD
Show:
© 2014 Microsoft