简体   繁体   English

根据同级节点的值选择XML节点

[英]Select XML node based on value of sibling node

I have a VB.NET 2013 program that reads from/writes to an xml document and I'm trying to figure out how to access a specific node when I know the text of one of the node's siblings. 我有一个VB.NET 2013程序,可读取/写入xml文档,当我知道节点的同级之一的文本时,我试图弄清楚如何访问特定的节点。 I've seen other posts that are similar to what I'm trying to do, but my xml structure is slightly different and I'm not sure how to apply those solutions to my document. 我看过其他我尝试做的事情类似的文章,但是我的xml结构略有不同,我不确定如何将这些解决方案应用于我的文档。

Here's a sample of my xml structure: 这是我的xml结构的示例:

<?xml version="1.0" encoding="utf-8"?>
<Books>
  <Book>
    <Title>Hansel and Gretel</Title>
    <Pages>221</Pages>
    <Price>3.5</Price>
    <Author>Grimm</Author>
  </Book>
  <Book>
    <Title>Green Eggs and Ham</Title>
    <Pages>145</Pages>
    <Price>5.25</Price>
    <Author>Dr. Seuss</Author>
  </Book>
</Books>

The examples I've seen in other posts have values in the node name <Title title="Green Eggs and Ham"> . 我在其他文章中看到的示例在节点名称<Title title="Green Eggs and Ham">具有值。 As you can see, mine doesn't. 如您所见,我没有。

I can't figure out how to use XPath, XPathNavigator, etc for my xml document to find a price, for instance if I know the title. 我无法弄清楚如何使用XPath,XPathNavigator等为我的xml文档查找价格,例如,如果我知道标题的话。

I see a lot of examples that look like: /Books/Book[@Title="Green Eggs and Ham"] , and I tried using that with XPathNavigator.SelectSingleNode(), but I can't figure out the correct VB.Net/XPath syntax to use with my xml format. 我看到很多示例,如下所示: /Books/Book[@Title="Green Eggs and Ham"] ,我尝试将其与XPathNavigator.SelectSingleNode()一起使用,但我无法找出正确的VB.Net / XPath语法与我的xml格式一起使用。

UPDATE (Example answer) Since I'm using this in a VB.NET project, I needed to know how to use the examples that Bogdan and ThW gave. 更新 (示例答案)由于我在VB.NET项目中使用了此示例,因此我需要知道如何使用Bogdan和ThW给出的示例。 Using my sample XML I did this to get the number of pages for the title "Hansel and Gretel": 使用示例XML,我这样做是为了获得标题“ Hansel and Gretel”的页面数:

Dim xpathDoc As XPathDocument = New XPathDocument([path_to_xml)
Dim xmlNav as XPathNavigator = xpathDoc.CreateNavigator()
Dim pages as String

pages = xmlNav.SelectSingleNode("/Books/Book[Title='Hansel and Gretel']/Pages").Value

Which, of course, returns "221" 当然返回“ 221”

The @ in XPath ist a shortcut for attribute:: . XPath中的@表示attribute::的快捷方式。 It selects attributes nodes (or more specific nodes on the attributes axis). 它选择属性节点(或属性轴上的更多特定节点)。

Your Title node is an element node. 您的Title节点是元素节点。 It is a child of the Book element node. 它是Book元素节点的子级。 child is the default axis, so it is optional. child是默认轴,因此它是可选的。 Basically /Books/Book is short for /child::Books/child::Book . 基本上/Books/Book/child::Books/child::Book缩写。

The part in [] is a condition for the nodes. []的部分是节点的条件。 [@Title="Green Eggs and Ham"] filters the node for an attribute Title with the given value. [@Title="Green Eggs and Ham"]过滤具有给定值的Title属性的节点。

To look for the child element node Title remove the @ . 要查找子元素节点Title删除@

/Books/Book[Title="Green Eggs and Ham"]

//Book/Title[text()='Green Eggs and Ham']/../Price

text() searches in value. text()搜索值。 .. selects parent element ..选择父元素

EDIT 编辑

Thanks ThW for explanation about . 感谢ThW对的解释. , removed from answer. ,从答案中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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