在此 XML 文档中查找值的正确 XPath 查询是什么? [英] What is the proper XPath query to find the value in this XML document?

查看:15
本文介绍了在此 XML 文档中查找值的正确 XPath 查询是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个 XML 文档...

Suppose I have this XML document...

<root>
  <str name="My node's attribute">My string's value</str>
</root>

我想根据元素name"属性的已知值获取str"元素的值,因此我使用以下 XPath 查询...

I want to get the value of the "str" element based on a known value of the elements "name" attribute so I use the following XPath query...

str[@name='My node's attribute']

但这对我不起作用,至少在经典的 ASP 和 C# 中不起作用,因为单引号当然与 XPath 语法中使用的单引号冲突.在这两种情况下都会抛出异常.

But that doesn't work for me at least not in classic ASP and C# because of the single quote which of course conflicts with the single quotes used in XPath syntax. An exception is thrown in both cases.

如果我有不能更改 XML 文档的限制,这里有什么可能的解决方案.

What is a possible solution here if I have the restriction that I can't change the XML document.

推荐答案

在 xpath 中使用 " 代替:-

Use " in the xpath instead:-

C#

 string xpath = "str[@name=\"My node's attribute\"]";

VBScript

 Dim xpath : xpath = "str[@name=""My node's attribute""]"

这篇关于在此 XML 文档中查找值的正确 XPath 查询是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆