如何在c#对象中反序列化这个嵌套的xml

时间:2014-06-04 08:42:58

标签: c# xml deserialization xml-deserialization

我正在使用silverlight来实现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。

2 个答案:

答案 0 :(得分:7)

您不需要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" ]
    }
  ]
}

答案 1 :(得分:1)

/ *     如果你想从xml到c#代码读取对象

1st create your xml file

2nd your c# code to DeSerializer

* /

//if you want read objects from xml to c# code

//1st create your xml file

<?xml version="1.0" encoding="utf-8" ?>
<FieldConfiguration>
  <A>
    <B>
      <Value>1</Value>  
    </B>
    <B>
      <Value>2</Value>
    </B>

    <ModuleID>1</ModuleID>
  </A>

  <A>
    <B>
      <Value>3</Value>
    </B>
    <B>
      <Value>4</Value>
    </B>

    <ModuleID>2</ModuleID>
  </A>
  </FieldConfiguration>


//2nd your c# code to DeSerializer

public List<A> FieldCollections;


        public SelftestAdv2()
        {

            XmlSerializer xs = new XmlSerializer(typeof(List<A>), new XmlRootAttribute("FieldConfiguration"));

            using (var streamReader = new StreamReader("fff.xml"))
            {

                FieldCollections = (List<A>)xs.Deserialize(streamReader);

            }


        }

//如果你想要相反,你有对象要保存在xml中

 public SelftestAdv2(int x)
        {
            B b1 = new B(); b1.v = 3;
            B b2 = new B(); b2.v = 4;

            B b3 = new B(); b3.v = 5;
            B b4 = new B(); b4.v = 6;

            A a1 = new A();a1.id = 1;
            a1.b.Add(b1);
            a1.b.Add(b2);

            A a2 = new A();a2.id = 2;
            a2.b.Add(b3);
            a2.b.Add(b4);

            List<A> listA = new List<A>();

            listA.Add(a1);
            listA.Add(a2);

            XmlSerializer xs = new XmlSerializer(typeof(List<A>), new XmlRootAttribute("FieldConfiguration"));

            using (var streamReader = new StreamWriter("fff.xml"))
            {

                xs.Serialize(streamReader,listA);

            }


        }
`