小编Moe*_*ard的帖子

XML字符串数组反序列化为不同的类型名称

我有以下C#类属性:

private List<string>  _accountTypes;

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

在类构造函数中初始化为:

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

反序列化时,我得到了这个:

<accountTypes>
   <string>OHGEE</string>
   <string>OHMY</string>
   <string>GOLLY</string>
   <string>GOLLYGEE</string>
</accountTypes>
Run Code Online (Sandbox Code Playgroud)

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

<accountTypes>
   <accountType>OHGEE</accountType>
   <accountType>OHMY</accountType>
   <accountType>GOLLY</accountType>
   <accountType>GOLLYGEE</accountType>
</accountTypes>
Run Code Online (Sandbox Code Playgroud)

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

c# xml xml-serialization deserialization

7
推荐指数
1
解决办法
6036
查看次数

标签 统计

c# ×1

deserialization ×1

xml ×1

xml-serialization ×1