Question

I have a xml in a string form. Let's say string .

<?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>

I have successfully deserialized and got its object like this:

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

}

And My main class is like this where i access the all the elements of 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);

Until here evrerything is fine but what if xml is nested and if i have xml like this:

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

In detail: if my xml string is like this:

<?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>

How will I get the element this time? because here parameter is the "parent" class and when we deserialize it doing this " Parameter parameter = (Parameter)deserializer.Deserialize(reader);" we will get an object of parameter and how I will access to the second <parameter>..</parameter><parameter>..How to get the data here..</parameter>?

Was it helpful?

Solution

If your xml document will look like this (which is a valid xml document):

<?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>

You will need another class for storing all elements <parameter>...</parameter>

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

And deserialize that document into that class.

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

To access those parameter elements you can simply use foreach loop e.g.:

Parameters p = new Parameters();

foreach (Parameter parameter in p.parameterList)
{
    Console.WriteLine(parameter.Name);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top