Serialization: Change the name of the root node without changing the class name
Goal
Take a class named "Item" and output its serialized XML as:
<Tem开发者_如何学Goplate><!--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
}
check the XmlRootAttribute class.
精彩评论