Home > Software design >  C# Reading specific node under parent ID
C# Reading specific node under parent ID

Time:10-05

So I know how to count the number of parent nodes under root. But I need to enumerate them, then select mod/rate based on the number of the parent in the order of the .xml document. It's important the code is retrieving based on the number, not the specific name of the mod/category in this case.

A function that can find/get based on its location (2nd), then retrieve the node's value. In this case the rate of "orange"

My fiddling with XmlDocument and XElement, and searching for a solution has yet any luck. Any help would be much appreciated! I'll clear up any confusion if necessary later, I know it's poorly worded.

<root>
    <mod category="apple">
        <detail>blank</detail>
        <idn>4807</idn>
        <rate>12</rate>
    </mod>

    <mod category="orange">
        <detail>blank</detail>
        <idn>10292</idn>
        <rate>20</rate>
    </mod>
</root>

CodePudding user response:

    XmlDocument XmlFile = new XmlDocument();
    XmlFile.Load( @"C:\XMLFile1.xml" );
    XmlNodeList XmlList = XmlFile.GetElementsByTagName( "mod" );
    //1st
    string rate1 = XmlList[0]["rate"].InnerText;
    string detail1 = XmlList[0]["detail"].InnerText;
    //2nd
    string rate2 = XmlList[1]["rate"].InnerText;
    string detail2 = XmlList[1]["detail"].InnerText;
  • Related