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 に答える