Sss*_*Sss 4 c# xml xml-deserialization deserialization
我使用silverlight ot实现xml的反序列化,如下所示:
String xmlString =
<attributes>
    <value>1</value>
    <showstatus>yes</showstatus>
    <disableothers>
        <disableother>
            <disablevalue>1</disablevalue>
            <todisable>skew</todisable>
            <todisable>skew_side</todisable>
        </disableother>
        <disableother>
            <disablevalue>0</disablevalue>
            <todisable>automodel</todisable>
        </disableother>
    </disableothers>
</attributes>
在我尝试实现这一目标的过程中,我觉得我在课堂上有一些东西.课程如下:
 [XmlRoot(ElementName = "attributes")]
    public class Attributes
    {
      [XmlElement("disableOthers")]
        public List<DisableOthers> DisableOthers { get; set; }
    }
[XmlRoot(ElementName = "disableOthers")]
    public class DisableOthers
    {
        [XmlElement("disableOthers")]
        public List<DisableOther> DisableOther { get; set; }
    }
 [XmlRoot(ElementName = "disableOther")]
    public class DisableOther
    {
        [XmlElement("disablingitem")]
        public int DisablingItem { get; set; }
        [XmlElement("todisable")]
        public int ToDisable { get; set; }
        [XmlElement("disablevalue")]
        public int DisableValue { get; set; }
    }
如果我的课程对应于给定的xml,那么有人可以纠正我吗?会是一个很大的帮助.
注意:问题确切的是当我创建父类的对象然后它给出"0"值.我已经尝试过了,然后我来到stackoverflow.
你不需要DisableOthers上课.只需使用属性XmlArrayItem属性:
[XmlArrayItem("disableother", IsNullable=false)]
[XmlArray("disableOthers")]
public DisableOther[] DisableOthers { get; set; }
完整映射看起来像:
[XmlRoot("attributes")]    
public class Attributes
{
    [XmlElement("value")]
    public byte Value { get; set; }
    [XmlElement("showstatus")]
    public string ShowStatus { get; set; }        
    [XmlArray("disableothers")]
    [XmlArrayItem("disableother", IsNullable = false)]
    public DisableOther[] DisableOthers { get; set; }
}
[XmlRoot("disableOther")]
public class DisableOther
{
    [XmlElement("disablevalue")]
    public byte DisableValue { get; set; }
    [XmlElement("todisable")]
    public string[] ToDisable { get; set; }
}
反序列化:
XmlSerializer serializer = new XmlSerializer(typeof(Attributes));
using (var reader = new StringReader(xmlString))
{
    var attributes = (Attributes)serializer.Deserialize(reader);
    attributes.Dump();
}
输出:
{
  Value: 1,
  ShowStatus: "yes",
  DisableOthers: [
    {
      DisableValue: 1,
      ToDisable: [ "skew", "skew_side" ]
    },
    {
      DisableValue: 0,
      ToDisable: [ "automodel" ]
    }
  ]
}