简体   繁体   中英

How can I print multiple nodes with same name under different parent nodes in an XML doc?

I am parsing an XML document that has information under separate nodes with the same name. As of now, when I use the xpath, I'm returning a total count of parent (2) and the node I'm looking for (6), but there is actually 5 under each one. The xml looks similar to this:

<library>
  <rack>13
    <shelf>shelf 1
      <book>
        <title>title1</title>
        <location>e6</location>
      </book>
      <book>
        <title>title2</title>
        <location>e7</location>
      </book >
      <book>
        <title>title3</title>
        <location>e8</location>
      </book >
    </shelf>
  </rack>
  <rack>13
    <shelf>shelf 2
      <book>
        <title>title4</title>
        <location>h5</location>
      </book>
      <book>
        <title>title5</title>
        <location>h6</location>
      </book>
      <book>
        <title>title6</title>
        <location>h7</location >
      </book>
    </shelf>
  </rack>
</library>

I am currently using the xpath to read the count of which come in as 6.

xmlNodeList library = doc.SelectNodes(//library);
xmlNodeList shelf= doc.SelectNodes(//library//shelf);
xmlNodeList location = doc.SelectNodes(//library//book//location);

if (library != null)
{
    for (int I=0; I < shelf.count; I++) //this should be 2
    {
        for (int j=0; j < location.count; j++) //this should be 6
        {
             Console.Writeline(shelf[j].innertext + " " + location[j].innertext);
        }
    }
}

So the problem is I get

shelf 1 e6
shelf 1 e7
shelf 1 e8
shelf 1 h5
shelf 1 h6
shelf 1 h7
shelf 2 e6
shelf 2 e7
shelf 2 e8
shelf 2 h5
shelf 2 h6
shelf 2 h7

but I want to see something more along the lines of

shelf 1 e6
shelf 1 e7
shelf 1 e8
shelf 2 h5
shelf 2 h6
shelf 2 h7

trying to get the actual XML document for clarity

Try

xmlNodeList shelf= doc.SelectNodes(//library//shelf);

if (library != null)
{
    for (int I=0; I < shelf.count; I++) //this should be 2
    {
        xmlNodeList location = shelf[I].SelectNodes(//book//location);

        for (int j=0; j < location.count; j++) //this should be however many nodes there are in the shelf
        {
             Console.Writeline(shelf[I].innertext + " " + location[j].innertext);
        }
    }
}

Using Xml Linq :

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

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

            var results = doc.Descendants("shelf").SelectMany(x => x.Descendants("location").Select(y => (x.FirstNode.ToString()).Trim() + " " + (string)y)).ToList();

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM