使用文本框中的字符串变量使用Xpath搜索Xml文件

时间:2013-10-12 11:48:00

标签: xml xpath

我是使用XPath的新手,非常感谢您的帮助。我想使用在登录页面的一个文本框中输入的变量来搜索类似于下面的XML文件。该变量用于验证正确的用户。我很难想出正确的表达来做到这一点。下面是我一直在使用的示例Xml文件的示例。我正在使用Visual Basic进行编程。

<Log>
    <customer>
        <customerid>12345</customerid>
        <password>Pass1</password>
        <login>00001</login>
        <image>Ducati1.jpg</image>
        <fname>Julie</fname>
        <lname>James</lname>
    </customer>
    <customer>
        <customerid>23456</customerid>
        <password>Pass2</password>
        <login>00002</login>
        <image>Ducati2.jpg</image>
        <fname>Bob</fname>
        <lname>Barnett</lname>
    </customer>
    <customer>
        <customerid>345678</customerid>
        <password>Pass3</password>
        <login>00003</login>
        <image>Ducati3.jpg</image>
        <fname>Callum</fname>
        <lname>Claw</lname>
    </customer>
</Log>

我尝试过的代码如下:  受保护的Sub Button2_Click(发送者为对象,e为System.EventArgs)处理Button2.Click

    Dim pass As String
    Dim user As String

    pass = lblPassword.Text
    user = lbluser.Text


    Dim doc As New XmlDocument
    Dim nodes As XmlNodeList


    doc.Load(Server.MapPath("App_Data\Customer.xml"))
    nodes = doc.SelectNodes("//customer[password = "pass")

    For Each node In nodes

        TextBox3.Text = node.SelectSingleNode("customerid").InnerText

    Next

End Sub

结束班

2 个答案:

答案 0 :(得分:0)

我不知道您使用vbs或vba的这种语言, 但我想你可以尝试这个xpath

 //customer[descendant::password[text()= 'Pass2']]

在这里测试http://www.xpathtester.com/obj/bbcf034f-1b3f-4a23-8e99-8f2b57044b46

当你把它放在doc.SelectNodes()

时,请注意单引号和双引号

答案 1 :(得分:0)

尝试以下XPATH表达式,它会提取customer pass2 password,然后需要customerid

//customer[password='pass2']/customerid

我怀疑不止一个用户可以拥有相同的password。最好使用customerid login,如下所述:

 //customer[login='00002']/customerid
相关问题