views:

45

answers:

1

Goal

Take a class named "Item" and output its serialized XML as:

<Template><!--some properties --></Template>

Problem

The root node is derived off the class name that is implementing IXmlSerializable.

    // By the time I get here 'writer' already has a root node
    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("Template");
         // write out the properties
        writer.WriteEndElement();
    }

So I wind up with XML that looks like

<Item><Template><!-- some properties --></Template></Item>

Question

Is there an attribute, a property I can override, or anything to get my desired effect (aside from changing the class name)?

Thanks!

Resolution thanks to Frederik!

Since the question is sort of answered in my comment of @Frederik Gheysels answer, I thought I would put it here so it doesn't get buried.

Just add an XmlRoot attribute to your class and this will change the output xml of the root node.

Example:

[XmlRoot("Template")]
public class Item : IXmlSerializable
{
   //Item's properties
}
+3  A: 

check the XmlRootAttribute class.

Frederik Gheysels
Awesome. Your answer helped me find the `XmlRoot` attribute. By adding `[XmlRoot("Template")]` above my class declaration `public class Item : IXmlSerializable`. I was able to get the desired effect.
Brandon Boone