如何在C#对象中反序列化此嵌套XML

4
我将使用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。


你能先自行验证一下吗? - Willem van Rumpt
你的属性中缺少值和状态,为什么不试一下呢? - rene
it gives "0" value” 的意思是什么?不需要加粗… - rene
2个回答

8

您不需要使用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
我正要写这个,是的,这是解决 OP 问题的方案。 - rosko
好的,让我先实现一下。很快会回来将您标记为答案。谢谢。 - Sss
@user234839,是的,抱歉。您还需要使用XmlArray属性而不是XmlElement。现在它可以正常工作-我刚刚检查过了。我会添加完整的映射。 - Sergey Berezovskiy
非常感谢。我有一个疑问。为什么在 public class DisableOther 前面加上 "[XmlRoot(ElementName = "disableOther")]" 而不是 "[XmlRoot("disableOther")]" 会导致异常? - Sss
2
@user234839 这两个定义之间没有区别。后者还将“disableOther”分配给ElementName属性。也许有其他问题导致异常。 - Sergey Berezovskiy
@SergeyBerezovskiy 如果我不必使用数组,只需使用List,那我是否仍然不需要另一个Disableothers类? - Sss

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);

            }


        }
`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接