XmlSerialization collection as an array

I am trying to serialize a custom class that should use multiple elements with the same name.
I tried using xmlarray, but it transfers them to other elements.

I want my xml to look like this.

<root>
     <trees>some text</trees>
     <trees>some more text</trees>
</root>

My code is:

[Serializable(), XmlRoot("root")]
public class test
{
      [XmlArray("trees")]
      public ArrayList MyProp1 = new ArrayList();

      public test()
      {
           MyProp1.Add("some text");
           MyProp1.Add("some more text");  
      }
}
+5
source share
2 answers

Try using [XmlElement("trees")]:

[Serializable(), XmlRoot("root")]
public class test
{
    [XmlElement("trees")]
    public List<string> MyProp1 = new List<string>();

    public test()
    {
        MyProp1.Add("some text");
        MyProp1.Add("some more text");
    }
}

Note. I changed ArrayListto List<string>to clear the output; in 1.1 StringCollectionwould be another option, although it has different case sensitivity rules.

+7
source

(edit: obsoleted - ([XmlElement]) - - xsd.exe)

xsd.exe - . xml, (foo.xml), :

xsd foo.xml
xsd foo.xsd /classes

foo.cs; , .

(: output snipped - )

0

All Articles