如何使用XmlSerializer反序列化嵌套结构

时间:2014-05-12 19:17:34

标签: c# .net xml deserialization xml-deserialization

我有一个字符串形式的xml。 让我们说字符串

<?xml version='1.0' encoding='utf-8' ?>
<parameter>
  <name>max_amount</name>
  <label>Max Amount</label>
  <unit>Millions</unit>
  <component>
    <type>Combo</type>
    <attributes>
      <type>Integer</type>
      <displayed>4</displayed>
      <selected>0</selected>
      <items>
        <item>5</item>
        <item>10</item>
        <item>20</item>
        <item>50</item>
      </items>
    </attributes>
  </component >
</parameter>

我已成功反序列化,并得到了这样的对象:

[XmlRoot(ElementName = "parameter")]
        public class Parameter
        {
            [XmlElement("name")]
            public string Name { get; set; }
            [XmlElement("label")]
            public string Label { get; set; }
            [XmlElement("unit")]
            public string Unit { get; set; }
            [XmlElement("component")]
            public Component Component { get; set; }
        } 



 [XmlRoot(ElementName = "component")]
    public class Component
    {
        [XmlElement("type")]
        public string Type { get; set; }
        [XmlElement("attributes")]
        public Attributes Attributes { get; set; }
    }





[XmlRoot(ElementName = "attributes")]
    public class Attributes
    {
        [XmlElement("type")]
        public string Type { get; set; }
        [XmlElement("displayed")]
        public string Displayed { get; set; }
        [XmlElement("selected")]
        public string Selected { get; set; }
        [XmlArray("items")]
        [XmlArrayItem("item", typeof(string))]
        public List<string> Items { get; set; }

    }

我的主类是这样的,我可以访问xml的所有元素:

    XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));
    XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
    Parameter parameter = (Parameter)deserializer.Deserialize(reader);
    foreach (var item in parameter.Component.Attributes.Items)
    {
        Debug.WriteLine(item);

    }
    Debug.WriteLine(parameter.Component.Type);
    Debug.WriteLine(parameter.Name);
    Debug.WriteLine(parameter.Label);
    Debug.WriteLine(parameter.Unit);

直到这里evrerything很好但是如果xml是嵌套的,如果我有这样的xml:

<parameter>
....
....
....
</parameter>
<parameter>
....
....
....
</parameter>

详细说明:如果我的xml字符串是这样的:

<?xml version='1.0' encoding='utf-8' ?>
<parameter>
  <name>max_amount</name>
  <label>Max Amount</label>
  <unit>Millions</unit>
  <component>
    <type>Combo</type>
    <attributes>
      <type>Integer</type>
      <displayed>4</displayed>
      <selected>0</selected>
      <items>
        <item>5</item>
        <item>10</item>
        <item>20</item>
        <item>50</item>
      </items>
    </attributes>
  </component >
</parameter>
<parameter>
  <name>max_amount_again</name>
  <label>Max Amount_again</label>
  <unit>Millions_again</unit>
  <component>
    <type>Combo</type>
    <attributes>
      <type>Integer</type>
      <displayed>41</displayed>
      <selected>01</selected>
      <items>
        <item>50</item>
        <item>100</item>
        <item>200</item>
        <item>500</item>
      </items>
    </attributes>
  </component >
</parameter>

这次我将如何获取元素?因为参数是“父”类,当我们反序列化它时执行此操作" Parameter parameter = (Parameter)deserializer.Deserialize(reader);"我们将获得parameter的对象以及我将如何访问第二个<parameter>..</parameter><parameter>..How to get the data here..</parameter>

1 个答案:

答案 0 :(得分:1)

如果您的xml文档看起来像这样(这是一个有效的xml文档):

<?xml version='1.0' encoding='utf-8' ?>
<parameters>
  <parameter>
    <name>max_amount</name>
    <label>Max Amount</label>
    <unit>Millions</unit>
    <component>
      <type>Combo</type>
      <attributes>
        <type>Integer</type>
        <displayed>4</displayed>
        <selected>0</selected>
        <items>
          <item>5</item>
          <item>10</item>
          <item>20</item>
          <item>50</item>
        </items>
      </attributes>
    </component >
  </parameter>
  <parameter>
    <name>max_amount_again</name>
    <label>Max Amount_again</label>
    <unit>Millions_again</unit>
    <component>
      <type>Combo</type>
      <attributes>
        <type>Integer</type>
        <displayed>41</displayed>
        <selected>01</selected>
        <items>
          <item>50</item>
          <item>100</item>
          <item>200</item>
          <item>500</item>
        </items>
      </attributes>
    </component >
  </parameter>
</parameters>

您需要另一个类来存储所有元素<parameter>...</parameter>

public class Parameters
{
    [XmlElement("parameter")]
    public List<Parameter> parameterList = new List<Parameter>();        
} 

将该文档反序列化为该类。

XmlSerializer deserializer = new XmlSerializer(typeof(Parameters));
using (TextReader reader = new StreamReader(@"C:\path\to\your\xml"))
{
    object obj = deserializer.Deserialize(reader);
    Parameters deserialized = (Parameters)obj;
}

要访问这些parameter元素,您只需使用foreach循环,例如:

Parameters p = new Parameters();

foreach (Parameter parameter in p.parameterList)
{
    Console.WriteLine(parameter.Name);
}