【发布时间】:2014-08-07 16:41:54
【问题描述】:
我正在尝试使用 c# 在 SILVERLIGHT 中进行反序列化。在我使用 XmlArray 之前它正在工作,但我想使用 List 代替,因为 我已经使用 List 完成了前面的部分(所以当我只使用数组时看起来很奇怪)。
我的xml是这样的:
<ps>
<disable_others>
<disable_other>
<disable_value>1</disable_value>
<to_disable>skew</to_disable>
<to_disable>skew_side</to_disable>
</disable_other>
<disable_other>
<disable_value>0</disable_value>
<to_disable>automodel</to_disable>
</disable_other>
</disable_others>
<ps>
我尝试反序列化它是:
[XmlRoot(ElementName = "ps")]
public class ps
{
[XmlArray("disable_others")]
[XmlArrayItem("disable_other", IsNullable = false)]
public List<string> Disable_Others { get; set; }
/* I know it can be done using the below but i have to use List not array:
[XmlArray("disable_others")]
[XmlArrayItem("disable_other", IsNullable = false)]
public Disable_Other[] Disable_Others { get; set; } */
}
[XmlRoot(ElementName = "disable_Others")]
public class Disable_Others
{
[XmlElement("disable_other")]
public List<Disable_Other> Disable_Other { get; set; }
}
[XmlRoot(ElementName = "Disable_Other")]
public class Disable_Other
{
[XmlElement("disablingitem")]
public int DisablingItem { get; set; }
[XmlElement("to_disable")]
public string[] To_Disable { get; set; }
[XmlElement("disable_value")]
public byte Disable_Value { get; set; }
}
有人可以帮我实现使用 List 而不是 array(XmlArray) 的目标吗?
【问题讨论】:
-
如果你已经在一个数组上工作了,你不能在数组上使用
Enumerable.ToList()吗? (还是说效率太低了?) -
@MatthewWatson 不,我必须手动完成。
-
您可以使用 xmlarray...与列表字段...并且您必须将您的类标记为 [serializable]。
-
@terrybozzio 你的意思是这样的: [XmlArray("disable_others")] [XmlArrayItem("disable_other", IsNullable = false)] public List
Disable_Others { get;放; } -
更像:[XmlArray("disable_others")] [XmlArrayItem(typeof(disable_other), IsNullable = false)] public List
Disable_Others { get;放; }.
标签: c# serialization xml-serialization deserialization xml-deserialization