c# – XML字符串数组反序列化为不同的类型名称
发布时间:2020-12-15 08:46:52 所属栏目:百科 来源:网络整理
导读:我有以下C#类属性: private Liststring _accountTypes;[XmlArray(ElementName = "accountTypes")]public Liststring AccountTypes{ get { return _accountTypes; } set { _accountTypes = value; }} 在类构造函数中初始化为: _accountTypes = new Liststri
我有以下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属性属性可用于获取我需要的内容? 解决方法
我认为您正在搜索[XmlArrayItem]属性.
试试这个: [XmlArray(ElementName = "accountTypes")] [XmlArrayItem("accountType")] public List<string> AccountTypes { get { return _accountTypes; } set { _accountTypes = value; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |