VB6 - 使用带反斜杠、下划线的 XPATH 选择单个节点 [英] VB6 - Select a single node using XPATH with backslashes, underscores

查看:24
本文介绍了VB6 - 使用带反斜杠、下划线的 XPATH 选择单个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我使用 VB 以来已经有一段时间了.我在某些 XML 上运行 XPATH 查询时遇到问题.

我有以下 XML:

<预><代码><配置><my.app.application><虚拟数据集><dummydataset tag="\\server\XX_YY_ZZ"/><dummydataset tag="default" mode="random"/></dummydatasets></my.app.application></配置>

我正在尝试使用 XPATH 查找虚拟数据集.要评估的 XPATH 是:

//configuration/my.app.application/dummydatasets/dummydataset [@tag="\\server\XX_YY_ZZ"]

所以这是双反斜杠,服务器",斜杠,XX_YY_ZZ".

VB6代码如下:

将 xmlDoc 淡化为新的 DOMDocument如果 xmlDoc.Load(path to xml) 然后将标签调暗为字符串theTag = "\\server\XX_YY_ZZ"Dim xPathQuery As StringxPathQuery = "//configuration/my.app.application/dummydatasets/dummydataset [@tag='" &标签"']"设置 xmlNode = xmlDoc.selectSingleNode(xPathQuery)万一

找不到标签.xmlNode 设置为Nothing.但是,使用 XML Copy Editor 等实用程序,我可以输入 XPATH 并找到节点.但是,如果我选择默认标签,代码就可以正常工作.

我不认为带有反斜杠和下划线的标签属性文本会有问题.希望我是错的.

任何想法,

谢谢

安德斯

解决方案

我对此进行了测试,问题出在反斜杠上.下划线没问题,但反斜杠会导致问题.

创建DOMDocument后需要指定SelectionLanguage:

xmlDoc.setProperty "SelectionLanguage", "XPath"

这里是 源页面.

Been a while since I used VB. I am having a problem running an XPATH query on some XML.

I have the following XML:

<configuration>
    <my.app.application>
        <dummydatasets>
            <dummydataset tag="\\server\XX_YY_ZZ" />
            <dummydataset tag="default" mode="random" />
        </dummydatasets>
    </my.app.application>
</configuration>

I am trying to find the dummy data set using XPATH. The XPATH to evaluate is:

//configuration/my.app.application/dummydatasets/dummydataset [@tag="\\server\XX_YY_ZZ"]

So that is double backslash, "server", slash, "XX_YY_ZZ".

The VB6 code is as follows:

Dim xmlDoc As New DOMDocument
If xmlDoc.Load(path to xml) Then

    Dim theTag As String
    theTag = "\\server\XX_YY_ZZ"
    Dim xPathQuery As String
    xPathQuery = "//configuration/my.app.application/dummydatasets/dummydataset [@tag='" & theTag & "']"
    Set xmlNode = xmlDoc.selectSingleNode(xPathQuery)

End If

The tag cannot be found. xmlNode is set to Nothing. However using a utility such as XML Copy Editor, I can enter the XPATH and the node is found. However, if I select the default tag, the code works fine.

I didn't think there would be a problem with the tag attribute's text with the backslash and underscores. I am hopefully wrong.

Any thoughts,

Thanks

Andez

解决方案

I tested this and the issue is with the backslashes. Underscores were fine but the backslashes caused issues.

You need to specify the SelectionLanguage in DOMDocument after it is created:

xmlDoc.setProperty "SelectionLanguage", "XPath"

Here is the source page.

这篇关于VB6 - 使用带反斜杠、下划线的 XPATH 选择单个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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