【问题标题】:How can I print multiple nodes with same name under different parent nodes in an XML doc?如何在 XML 文档的不同父节点下打印多个具有相同名称的节点?
【发布时间】:2020-01-13 19:17:35
【问题描述】:

我正在解析一个 XML 文档,该文档在具有相同名称的不同节点下具有信息。截至目前,当我使用 xpath 时,我返回了父节点 (2) 和我正在寻找的节点 (6) 的总数,但实际上每个节点下都有 5 个。 xml 看起来与此类似:

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

我目前正在使用 xpath 来读取其中的计数为 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);
        }
    }
}

所以问题是我明白了

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

但我想看到更多类似

的东西
shelf 1 e6
shelf 1 e7
shelf 1 e8
shelf 2 h5
shelf 2 h6
shelf 2 h7

为了清晰起见,尝试获取实际的 XML 文档

【问题讨论】:

    标签: c# xml parsing


    【解决方案1】:

    试试

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

    【讨论】:

      【解决方案2】:

      使用 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();
      
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多