8

我有以下C#类属性:

private List<string>  _accountTypes;

[XmlArray(ElementName = "accountTypes")]
public List<string> AccountTypes
{
   get { return _accountTypes; }
   set { _accountTypes = value; }
}

在类构造函数中这样初始化:

_accountTypes       = new List<string>( new string[] { "OHGEE", "OHMY", "GOLLY", "GOLLYGEE" });

当反序列化我得到这个:

<accountTypes>
   <string>OHGEE</string>
   <string>OHMY</string>
   <string>GOLLY</string>
   <string>GOLLYGEE</string>
</accountTypes>

如果我能得到这个,我会喜欢它:

<accountTypes>
   <accountType>OHGEE</accountType>
   <accountType>OHMY</accountType>
   <accountType>GOLLY</accountType>
   <accountType>GOLLYGEE</accountType>
</accountTypes>

如果不创建“accountType”类型的子类,怎么办?是否有任何 XML 属性属性可用于获取我需要的内容?

4

1 回答 1

15

我认为您正在寻找[XmlArrayItem]属性。

尝试这个:

[XmlArray(ElementName = "accountTypes")]
[XmlArrayItem("accountType")]
public List<string> AccountTypes
{
   get { return _accountTypes; }
   set { _accountTypes = value; }
}
于 2011-02-25T01:13:43.990 回答