Reputation: 8997
I have the snippet of code below that the serialize a simple instance of a classPerson
to <Person attribute="value" />
using IXmlSerializable
:
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
public class Person : IXmlSerializable
{
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader xmlReader)
{
throw new System.NotImplementedException();
}
public void WriteXml(XmlWriter xmlWriter)
{
xmlWriter.WriteAttributeString("attribute", "value");
}
}
class Program
{
public static void Main()
{
var xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
using (var xmlTextWriter = XmlWriter.Create(Console.Out, xmlWriterSettings))
{
var xmlSerializer = new XmlSerializer(typeof(Person));
var person = new Person();
xmlSerializer.Serialize(xmlTextWriter, person);
}
}
}
I am looking for a way to modify the element name of Person
to person
, how can I do that?
Upvotes: 1
Views: 1067
Reputation: 1500155
You can use XmlRootAttribute
to specify the element name for the root element:
[XmlRoot(ElementName = "person")]
public class Person : IXmlSerializable
{
...
}
Upvotes: 2