Note that there are some explanatory texts on larger screens.

plurals
  1. POXmlSerializer List Item Element Name
    primarykey
    data
    text
    <p>I have a class <code>PersonList</code></p> <pre><code>[XmlRoot("Persons")] PersonList : List&lt;Human&gt; </code></pre> <p>when I serialize this to XML, by default it will produce something like this:</p> <pre><code>&lt;Persons&gt; &lt;Human&gt;...&lt;/Human&gt; &lt;Human&gt;...&lt;/Human&gt; &lt;/Persons&gt; </code></pre> <p>My question is what needs to be done in order to change element <code>Human</code> to <code>Person</code> in the output? so the output would be :</p> <pre><code>&lt;Persons&gt; &lt;Person&gt;...&lt;/Person&gt; &lt;Person&gt;...&lt;/Person&gt; &lt;/Persons&gt; </code></pre> <p>and, how to deserialize the above XML to the <code>PersonList</code> class object?</p> <p>Per Nick's advice, Here is my testing code:</p> <pre><code>[XmlRoot("Persons")] public class Persons : List&lt;Human&gt; { } [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()); } } </code></pre> <p>The output of the testing code is:</p> <pre><code>&lt;Persons&gt; &lt;Human&gt; &lt;Name&gt;John&lt;/Name&gt; &lt;/Human&gt; &lt;Human&gt; &lt;Name&gt;Peter&lt;/Name&gt; &lt;/Human&gt; &lt;/Persons&gt; </code></pre> <p>As the output shows, the <code>[XmlRoot("Person")]</code> on <code>Human</code> does not change the tag to <code>Person</code> from <code>Human</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload