在Xpath中转义字符 [英] Escape character in Xpath

查看:448
本文介绍了在Xpath中转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好


我收到此错误"" Expected token']'found'NAME'"主要是因为我使用的xpath查询包含一个特殊字符 - 单引号ex:xpath = \ [@ source = men's],我正在使用MSXML2 xpath进行解析。我们如何在VB.Net中逃避这些特殊字符
?这是VB6的升级项目。



谢谢

解决方案

< blockquote>

首先使用.NET,你根本不应该使用MSXML,而应该使用System.Xml和System.Xml.XPath中的纯托管XML和XPath类。


对于XPath语法,XPath 1.0(这是MSXML和.NET支持的)没有引号的转义机制,所以你唯一的方法是尽可能长地交替双引号和单引号,例如

 Dim xml As String ="< root>< foo source ="" men's"" />< / root>" 
Dim value As String =" men's"
Dim doc As New XmlDocument()
doc.LoadXml(xml)
For each foo As XmlElement in doc.SelectNodes(String.Format(" // foo [@source =" " {0}""]",value))
Console.WriteLine(foo.OuterXml)
下一步



如果您仍然遇到问题,请发布确切的代码。

Hi

I'm getting this error ""Expected token ']' found 'NAME'" mainly because the xpath query that I'm using contains a special character - single quote ex: xpath = \[@source=men's] and I'm using MSXML2 xpath for parsing. How do we escape such special characters in VB.Net? This is a upgraded project from VB6.

Thanks

解决方案

First of all with .NET you shouldn't use MSXML at all, instead you should use the pure managed XML and XPath classes in System.Xml and System.Xml.XPath.

As for the XPath syntax, XPath 1.0 (which is what both MSXML and .NET support) does not have an escape mechanism for quotes so the only approach you have is alternating double and single quotes as long as possible e.g.

        Dim xml As String = "<root><foo source=""men's""/></root>"
        Dim value As String = "men's"
        Dim doc As New XmlDocument()
        doc.LoadXml(xml)
        For Each foo As XmlElement In doc.SelectNodes(String.Format("//foo[@source = ""{0}""]", value))
            Console.WriteLine(foo.OuterXml)
        Next

If you still have problems then please post the exact code.


这篇关于在Xpath中转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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