本文介绍了如何反序列化在C#中的对象这个嵌套的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Silverlight OT实现XML的deserialisation看起来像这样:

I am using silverlight ot achieve deserialisation of xml which looks like this:

字符串的xmlString =

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>

在我试图做到这一点我觉得我有类的东西。这些类是如下:

In my attempt to achieve this i feel like i have something in the classes. The classes are as below:

 [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? 。将是一个很大的帮助。

Could some one please correct me if my classes are correct corresponding to the given xml ? Would be a big help.

请注意:确切的问题是,当我创建了父类的对象,那么它给0值。 ,我已经尝试过了,然后我来到这里的计算器。

NOTE: The problem exact is when i created object of the parent class then it gives "0" value. And i have already tried it then i came here on stackoverflow.

推荐答案

您不需要 DisableOthers 类。只要使用财产 XmlArrayItem 属性:

You don't need DisableOthers class. Just use property with XmlArrayItem attribute:

[XmlArrayItem("disableother", IsNullable=false)]
[XmlArray("disableOthers")]
public DisableOther[] DisableOthers { get; set; }



完整映射如下:

Complete mapping looks like:

[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; }
}



反序列化:

Deserialization:

XmlSerializer serializer = new XmlSerializer(typeof(Attributes));
using (var reader = new StringReader(xmlString))
{
    var attributes = (Attributes)serializer.Deserialize(reader);
    attributes.Dump();
}



输出:

Output:

{
  Value: 1,
  ShowStatus: "yes",
  DisableOthers: [
    {
      DisableValue: 1,
      ToDisable: [ "skew", "skew_side" ]
    },
    {
      DisableValue: 0,
      ToDisable: [ "automodel" ]
    }
  ]
}

这篇关于如何反序列化在C#中的对象这个嵌套的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:38