Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XML字符串数组反序列化为不同的类型名称_C#_Xml_Xml Serialization_Deserialization - Fatal编程技术网

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

C# XML字符串数组反序列化为不同的类型名称,c#,xml,xml-serialization,deserialization,C#,Xml,Xml Serialization,Deserialization,我有以下C#类属性: private List<string> _accountTypes; [XmlArray(ElementName = "accountTypes")] public List<string> AccountTypes { get { return _accountTypes; } set { _accountTypes = value; } } private List\u账户类型; [XmlArray(ElementName=“

我有以下C#类属性:

private List<string>  _accountTypes;

[XmlArray(ElementName = "accountTypes")]
public List<string> AccountTypes
{
   get { return _accountTypes; }
   set { _accountTypes = value; }
}
private List\u账户类型;
[XmlArray(ElementName=“accountTypes”)]
公共列表帐户类型
{
获取{return\u accountTypes;}
设置{u accountTypes=value;}
}
在类构造函数中初始化如下:

_accountTypes       = new List<string>( new string[] { "OHGEE", "OHMY", "GOLLY", "GOLLYGEE" });
\u accountTypes=新列表(新字符串[]{“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>

OHGEE
欧姆
天哪
天哪
如果我能得到这个,我会很高兴的:

<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>

OHGEE
欧姆
天哪
天哪

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

我想您正在搜索
[XmlArrayItem]
属性

试试这个:

[XmlArray(ElementName = "accountTypes")]
[XmlArrayItem("accountType")]
public List<string> AccountTypes
{
   get { return _accountTypes; }
   set { _accountTypes = value; }
}
[XmlArray(ElementName=“accountTypes”)]
[XmlArrayItem(“账户类型”)]
公共列表帐户类型
{
获取{return\u accountTypes;}
设置{u accountTypes=value;}
}