Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 617113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:23:58+00:00 2026-05-13T18:23:58+00:00

I have a class PersonList [XmlRoot(Persons)] PersonList : List<Human> when I serialize this to

  • 0

I have a class PersonList

[XmlRoot("Persons")]
PersonList : List<Human>

when I serialize this to XML, by default it will produce something like this:

<Persons>
  <Human>...</Human>
  <Human>...</Human>
</Persons>

My question is what needs 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?

Per Nick’s advice, Here is my testing code:

[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")] on Human does not change the tag to Person from Human.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:23:58+00:00Added an answer on May 13, 2026 at 6:23 pm

    I don’t think there is a way for you to control the name of the generated array elements.

    If you can however wrap the Persons collection inside another class you will then have complete control over the generated output using XmlArrayAttribute and XmlArrayItemAttribute.

    If you cannot create this new class you can resort to implementing IXmlSerializable, but this is much more complex.

    An example for the first alternative follows:

    [XmlRoot("Context")]
    public class Context
    {
        public Context() { this.Persons = new Persons(); }
    
        [XmlArray("Persons")]
        [XmlArrayItem("Person")]
        public Persons Persons { get; set; }
    }
    
    public class Persons : List<Human> { }
    
    public class Human
    {
        public Human() { }
        public Human(string name) { Name = name; }
        public string Name { get; set; }
    }
    
    class Program
    {
        public static void Main(string[] args)
        {
            Context ctx = new Context();
            ctx.Persons.Add(new Human("john"));
            ctx.Persons.Add(new Human("jane"));
    
            var writer = new StringWriter();
            new XmlSerializer(typeof(Context)).Serialize(writer, ctx);
    
            Console.WriteLine(writer.ToString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a util class like this: public class Util { static public List<Person>
I have class like this below shown. which contains the shopping items where the
I have class that looks like this: class A { public: class variables_map vm
Ok, I'me getting real dumb with this one.... I have this class: Public Class
I have a list of items of a custom class. That class contains an
In short, I have an object called PersonList that has a List of Person
I have class method that returns a list of employees that I can iterate
I have the following list: class Person { public String Name { get; set;
I have class that is a DataContract and the member values are holding default
I have class called GroupSelect and made a collection List(Of GroupSelect)() . Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.