XmlSerialization Collection as Array

5 .net c# xml-serialization

我正在尝试序列化需要使用多个同名元素的自定义类.
我尝试过使用xmlarray,但它将它们包含在另一个元素中.

我希望我的xml看起来像这样.

<root>
     <trees>some text</trees>
     <trees>some more text</trees>
</root>
Run Code Online (Sandbox Code Playgroud)

我的代码:

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

      public test()
      {
           MyProp1.Add("some text");
           MyProp1.Add("some more text");  
      }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 7

尝试使用[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");
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我改变ArrayListList<string>清理输出; 在1.1中,StringCollection将是另一种选择,尽管它具有不同的区分大小写规则.