How to change the Array Element names in XmlSerialization?

Posted by MSDN Geek on Stack Overflow See other posts from Stack Overflow or by MSDN Geek
Published on 2010-04-15T14:33:39Z Indexed on 2010/04/15 16:53 UTC
Read the original article Hit count: 163

Filed under:
|

Hi Guys,

Consider the following code:

[Serializable]
public class Human
{
    public string Name { get; set; }
}

Then,

        using (MemoryStream ms = new MemoryStream())
        {   
            Human[] mans = new Human[] { 
                new Human() { Name = "Moim" }
                    };

            XmlSerializer xs = new XmlSerializer(typeof(Human[]));
            xs.Serialize(ms, mans);
            string s = System.Text.ASCIIEncoding.ASCII.GetString(ms.ToArray());
        }

At this point, the variable s will hold a value like,

<?xml version="1.0"?>
<ArrayOfHuman xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Human>
    <Name>Moim</Name>
  </Human>
</ArrayOfHuman>

Now all I need to do is, changing the xml array root element 'ArrayOfHuman' to something like 'MyFavoriteArrayRootName'. I have seen the IXmlSerializable interface but, that skips the root element name. Anybody has got any idea how to achieve this?

All comments will be greatly appreciated.

Best regards.

© Stack Overflow or respective owner

Related posts about c#

Related posts about xml-serialization