5

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

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

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

我的代码:

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

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

2 回答 2

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");
    }
}

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

于 2009-01-31T23:09:04.553 回答
0

(编辑:已过时-我的第二篇文章[XmlElement])是要走的路-我将把它留给后代使用xsd.exe

xsd.exe是你的朋友。将所需的 xml 复制到文件 ( foo.xml) 中,然后使用:

xsd foo.xml
xsd foo.xsd /classes

现在阅读foo.cs;你可以直接使用它,也可以仅仅为了灵感。

(编辑:输出被剪断 - 不再有帮助)

于 2009-01-31T11:03:07.403 回答