HI
Hope someone can help me on this.
Say, I have an object of class
[XmlRoot("Persons")]
PersonList : List<Human>
, when serialize this object to xml, by defualt it will produce something like this:
<Persons><Human>.......</Human><Human>.......</Human></Persons>
My question is what need to be done in order to change element Human to Person in the output? so the output would be :
<Persons><Person>.......</Person><Person>.......</Person></Persons>
and, how to deserialize the above xml to the PersonList class object?
Many Thanks,
Q
Per Nick's advice, Here are my testing codes:
[XmlRoot("Persons")]
public class Persons : List<Human>
{
}
[XmlRoot("Person")]
public class Human
{
public Human()
{
}
public Human(string name)
{
Name = name;
}
[XmlElement("Name")]
public string Name { get; set; }
}
void TestXmlSerialize()
{
Persons personList = new Persons();
personList.Add(new Human("John"));
personList.Add(new Human("Peter"));
try
{
using (StringWriter writer = new StringWriter())
{
XmlSerializer serializer = new XmlSerializer(typeof(Persons));
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
XmlWriter xmlWriter = XmlWriter.Create(writer, settings);
serializer.Serialize(xmlWriter, personList, namespaces);
Console.Out.WriteLine(writer.ToString());
}
}
catch (Exception e)
{
Console.Out.WriteLine( e.ToString());
}
}
The output of the testing code is:
<Persons><Human><Name>John</Name></Human><Human><Name>Peter</Name></Human></Persons>
As the output shows, the [XmlRoot("Person")] attribute on Human class does not change the tag to Person from Human.
Any thoughts?
Thanks,
Q