如何使用同名节点的所有子节点填充 XmlNodeList?

发布于 2024-11-18 18:42:49 字数 1041 浏览 3 评论 0原文

给定一个包含以下 XML 片段的 XmlNode,如何执行我用书籍节点填充XmlNodeList?

XMLNode nodeLibrary 包含:

<library>
  <book>
      <title>Three Little Pigs</title>
  </book>
  <book>
      <title>Batman</title>
  </book>
  <address>123 Main St.</address>
  <phone>111-111-1111</phone>
</library>

这应该很容易,但我无法弄清楚:

A)无法将类型“System.Xml.XmlElement”隐式转换为“System.Xml.XmlNodeList”:

XmlNodeList books = nodeLibrary[“book”];

我猜上面的方法属性快捷方式假设有一个名为 book 的子项,而不是多个!

B) XmlNode 没有 GetChildren() 方法:

XmlNodeList books = nodeLibrary.GetChildren("book");

C) XmlNode 的 ChildNodes 属性获取所有子节点,不仅仅是书籍节点。

D) 我尝试使用 SelectNodes() 方法,但根是较大的文档,而不是之前使用 SelectNodes 从较大文档中选择的当前 XmlNode 中的库片段。

有什么想法吗? 皮特

Given a XmlNode containing the following XML fragment, how do I fill XmlNodeList with book nodes?

XMLNode nodeLibrary contains:

<library>
  <book>
      <title>Three Little Pigs</title>
  </book>
  <book>
      <title>Batman</title>
  </book>
  <address>123 Main St.</address>
  <phone>111-111-1111</phone>
</library>

This should be really easy but I can't figure it out:

A) Cannot implicitly convert type 'System.Xml.XmlElement' to 'System.Xml.XmlNodeList':

XmlNodeList books = nodeLibrary["book"];

I guess the method property shortcut above assumes there's a SINGLE child named book, not multiple!

B) XmlNode doesn't have a GetChildren() method:

XmlNodeList books = nodeLibrary.GetChildren("book");

C) XmlNode's ChildNodes property gets ALL children, not just book nodes.

D) I tried using SelectNodes() method but the root is the larger document, not the library fragment in the current XmlNode that was selected from a larger document earlier using SelectNodes.

Any ideas?
Pete

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2024-11-25 18:42:49

您可以使用 SelectNodes,并在 XPath 查询中传递“.”从该节点开始搜索:

public class StackOverflow_6618097
{
    const string XML = @"<buildings>
 <library>
  <book>
   <title>Three Little Pigs</title>
  </book>
  <book>
   <title>Batman</title>
  </book>
  <address>123 Main St.</address>
  <phone>111-111-1111</phone>
  <hidden>
   <book>
    <title>The Hidden Treasure</title>
   </book>
  </hidden>
 </library>
 <bookstore>
  <book>
   <title>Cat in the Hat</title>
  </book>
 </bookstore>
</buildings>";

    public static void Test()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XML);
        XmlNode libraryNode = doc.SelectSingleNode("//library");
        XmlNodeList libraryBooks = libraryNode.SelectNodes(".//book");
        Console.WriteLine("Books: {0}", libraryBooks.Count);
        foreach (XmlNode node in libraryBooks)
        {
            Console.WriteLine(node.OuterXml);
        }
    }
}

You can use SelectNodes, and in the XPath query pass a '.' to start searching from that node:

public class StackOverflow_6618097
{
    const string XML = @"<buildings>
 <library>
  <book>
   <title>Three Little Pigs</title>
  </book>
  <book>
   <title>Batman</title>
  </book>
  <address>123 Main St.</address>
  <phone>111-111-1111</phone>
  <hidden>
   <book>
    <title>The Hidden Treasure</title>
   </book>
  </hidden>
 </library>
 <bookstore>
  <book>
   <title>Cat in the Hat</title>
  </book>
 </bookstore>
</buildings>";

    public static void Test()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(XML);
        XmlNode libraryNode = doc.SelectSingleNode("//library");
        XmlNodeList libraryBooks = libraryNode.SelectNodes(".//book");
        Console.WriteLine("Books: {0}", libraryBooks.Count);
        foreach (XmlNode node in libraryBooks)
        {
            Console.WriteLine(node.OuterXml);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文