0

A simple class:

public class AddressDetails
{
    public string DbServerName { get; set; }
}

So I just created a list of two object of it:

    List<AddressDetails> AdrsList = new List<AddressDetails>();

    AddressDetails detail1 = new AddressDetails();
    detail1.DbServerName = "2k8";
    AdrsList.Add(detail1);

    AddressDetails detail2 = new AddressDetails();
    detail2.DbServerName = "2k8R2D3";
    AdrsList.Add(detail2);

    Serialize(AdrsList);

And wanted to serialize it, with my method below:

public void Serialize(List<AddressDetails> details)
{
    XmlSerializer serializer = new XmlSerializer(typeof(AddressList));
    using (TextWriter writer = new StreamWriter(@"C:\TEMP\MyXML.xml"))
    {
        serializer.Serialize(writer, details);
    }
}

The XML that it generates its element has the word "ArrayOf" in it, why? I don't want it, where is it coming from and how to fix it?

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAddressDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <AddressDetails>
    <DbServerName>2k8</DbServerName>
  </AddressDetails>
  <AddressDetails>
    <DbServerName>2k8R2D3</DbServerName>
  </AddressDetails>
</ArrayOfAddressDetails>
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Bohn
  • 26,091
  • 61
  • 167
  • 254
  • 2
    Well what should it be named instead, `ListOfAddressDetails`? you have to name the node that represents the collection and a list is an Array with some abstractions on top of it that copies it to a larger array when it gets full. – Scott Chamberlain Feb 24 '16 at 20:25
  • 1
    The XML serializer names it that by default, for the reasons Scott mentions above. Look at [this SO question](http://stackoverflow.com/questions/3129001/how-to-rename-arrayof-xml-attribute-that-generated-after-serializing-list-of-o) on how to change the name. – Icemanind Feb 24 '16 at 20:25

0 Answers0