0

<String>
  <Object>
   <Name>Whatever</Name>
  </Object>
</String>

<Array>
        <Name>Max</Name>
        <Dimsize>8</Dimsize>
        <Cluster>
            <Name>Maximums</Name>
            <NumElts>3</NumElts>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
        </Cluster>
        <Cluster>
            <Name>Maximums</Name>
            <NumElts>3</NumElts>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
        </Cluster>
        <Cluster>
            <Name>Maximums</Name>
            <NumElts>3</NumElts>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
            <U16>
                <Name>Max</Name>
                <Val>0</Val>
            </U16>
        </Cluster>
    </Array>

Hi,

I have an XML string above and I would like to parse its data into array[item].

As you can see, there are three Cluster node. I want to iterate through the XML text and store the content inside Cluster to Array item[0], item[1] and item[2].

I have the following code so far but it only print out the data from the first node only. How can I get all the data from the remaining node?

        var xmlStr = File.ReadAllText("myxml.xml");
        var str = XElement.Parse(xmlStr);

            for (int i = 0; i < 3; i++)
        {
            var txt = str.XPathSelectElement("Array/Cluster");
            var values = txt.Value;
            Console.WriteLine(values);
            myChannelArray[i] = values;
        }
CB4
  • 690
  • 3
  • 13
  • 25
  • 3
    Possible duplicate of [How does one parse XML files?](http://stackoverflow.com/questions/55828/how-does-one-parse-xml-files) – Nasreddine May 16 '16 at 17:06
  • 1
    There's a bunch of libraries that can parse XML faster than you'll ever achieve yourself. I'd suggest you use one instead of reinventing the wheel. – Pierre-Luc Pineault May 16 '16 at 17:07

2 Answers2

1

Instead of parsing each item manually you can use XmlElement deserialization into an object, which is probably the easiest. Just setup an object model that mimics your Xml model.

You can use something like the following:

var original = XElement.Parse(xml);

//Find the target node where your Array is
var arrayElement = original.Element("Array").ToString();
Data data;
using (XmlReader reader = XmlReader.Create(new StringReader(arrayElement)))
{
    XmlSerializer serializer = new XmlSerializer(typeof(Data));
    data = serializer.Deserialize(reader) as Data;
}

Your model can be something like the following:

[XmlRoot("Array")]
public class Data
{
    public string Name { get; set; }
    public string DimSize { get; set; }

    [XmlElement("Cluster")]
    public List<Cluster> Cluster { get; set; }
}

public class Cluster
{
    public string Name { get; set; }
    public int NumElts { get; set; }

    [XmlElement("U16")]
    public List<U16> U16 { get; set; }
}

public class U16
{
    public string Name { get; set; }
    public string Val { get; set; }
}
loopedcode
  • 4,863
  • 1
  • 21
  • 21
0

System.Xml namespace gives methods to parse xml.
Example:

var xDoc = new System.Xml.XmlDocument();
xDoc.Load("myxml.xml");
var xList = xDoc.SelectNodes("//Array/Cluster");//XmlNodeList
foreach(XmlNode xNode in xList)
{
   var xName = xNode.SelectSingleNode("Name");//XmlNode 
   string name = xNode.InnerText;
   // and so on
}
Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36