Home > Mobile >  Looping through items in XML and read desired elements
Looping through items in XML and read desired elements

Time:07-18

I have something like this:

<ITEMS ASOF_DATE="6/2/2022" RECORDS="1" CREATE_DATE="6/3/2022" >
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomeValue>-808</SomeValue>
    </ITEM>
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomeValue>-808</SomeValue>
        <SomeOtherValue>-808</SomeOtherValue>
    </ITEM> 
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomethingElse>234</SomethingElse>
    </ITEM> 
</ITEMS>

It can have multiple <ITEM> items and those can have multiple elements under it for example first item has three elements, second one has four , third one has three in this example. I want to loop through all of these <ITEM> items and read some specific elements that I am interested in. For example <port_name> and <bench_name>

But I can't figure out how to do the loop. For example root.SelectNodes("ITEMS") in my code below isn't what I am hoping to be able to loop through its items.

 XmlDocument doc = new XmlDocument();
        doc.Load(myFilePath.ToString());
        XmlElement root = doc.DocumentElement;
        var lineItems = root.InnerXml;

        XmlNodeList nodes = root.SelectNodes("ITEMS");
        foreach (XmlNode node in nodes)
        {
            var somethingElse = node.SelectNodes("ITEM");
        }

CodePudding user response:

Try using the Elements() or Descendants() methods to iterate your root element.

static void Main(string[] args)
{
    var root = XElement.Parse(source);
    foreach (var item in root.Elements("ITEM"))
    {
        foreach (var entry in item.Elements())
        {
            Console.WriteLine($"{entry.Name.LocalName} = {(string)entry}");
        }
    }
}
const string source =
@"<ITEMS ASOF_DATE=""6/2/2022"" RECORDS=""1"" CREATE_DATE=""6/3/2022"" >
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomeValue>-808</SomeValue>
    </ITEM>
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomeValue>-808</SomeValue>
        <SomeOtherValue>-808</SomeOtherValue>
    </ITEM> 
    <ITEM>
        <port_name>95512M</port_name>
        <bench_name>LEHSECUR</bench_name>
        <SomethingElse>234</SomethingElse>
    </ITEM> 
</ITEMS>";
}

console output

CodePudding user response:

Using XML Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication23
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement item in doc.Descendants("ITEM"))
            {
                foreach (XElement child in item.Elements())
                {
                    Console.WriteLine("{0} = {1}", child.Name.LocalName, (string)child);
                }
            }
            Console.ReadLine();

        }
    }

 
}
  • Related