根据父元素和值获取XML属性值

时间:2014-08-13 18:09:59

标签: xml vb.net

我想将下面xml中属性的值赋给变量。但是我无法弄清楚引用我想要的属性的语法。例如,我如何引用名为“qatest”的属性

  • “asr”的子元素
  • 的值为“en-US”

    <asr>
        <item qatest="http://app05-test.qa.example.com:8080/asr/status" qadmz="http://qadmz92.dmz.example.com:8080/asr/status">en-US</item>
        <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">en-GB</item>
        <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">fr-FR</item>
        <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">it-IT</item>
        <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">de-DE</item>
        <item qatest="http://app10-test.qa.example.com:8080/asr/status"  qadmz="http://qadmz97.dmz.example.com:8080/asr/status">pt-BR</item>
        <item qatest=http://app10-test.qa.example.com:8080/asr/status"" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">ru-RU</item>
        <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">zh-CN</item>
        <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">ja-JP</item>
        <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">ko-KR</item>
    </asr>
    
    <lmtt>
        <item qatest="http://app14-test.qa.example.com:8080/lmtt/status" qadmz="http://qadmz94.dmz.example.com:8080/lmtt/status">all</item>
    </lmtt>
    

到目前为止,我找到了这段代码,但它不够具体,无法知道父元素和属性与哪些值相关

attrValue = xml.GetAttrValue("qatest")

我仍然在使用XML术语,所以请问我是否使用了错误的术语。

2 个答案:

答案 0 :(得分:0)

如果您想获得单个值结果(如果不存在重复项),请尝试以下LINQ方法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim XDocument1 As XDocument =
        <?xml version="1.0" encoding="UTF-8"?>
        <Data>
            <asr>
                <item qatest="http://app05-test.qa.example.com:8080/asr/status" qadmz="http://qadmz92.dmz.example.com:8080/asr/status">en-US</item>
                <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">en-GB</item>
                <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">fr-FR</item>
                <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">it-IT</item>
                <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">de-DE</item>
                <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">pt-BR</item>
                <item qatest="http://app10-test.qa.example.com:8080/asr/status" qadmz="http://qadmz97.dmz.example.com:8080/asr/status">ru-RU</item>
                <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">zh-CN</item>
                <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">ja-JP</item>
                <item qatest="http://app12-test.qa.example.com:8080/asr/status" qadmz="http://qadmz98.dmz.example.com:8080/asr/status">ko-KR</item>
            </asr>
            <lmtt>
                <item qatest="http://app14-test.qa.example.com:8080/lmtt/status" qadmz="http://qadmz94.dmz.example.com:8080/lmtt/status">all</item>
            </lmtt>
        </Data>

    Dim ElementName1 As String = "asr"
    Dim ElementValue1 As String = "en-US"
    Dim AttributeName1 As String = "qatest"
    Dim AttributeValue1 As String = ""

    Dim XElement1 As XElement = (From X1 In XDocument1.Descendants Where X1.Name = ElementName1)(0)

    If Not XElement1 Is Nothing Then
        Dim XElement2 As XElement = (From X2 In XElement1.Elements Where X2.Value = ElementValue1)(0)

        If Not XElement2 Is Nothing AndAlso Not XElement2.Attribute(AttributeName1) Is Nothing Then
            AttributeValue1 = XElement2.Attribute(AttributeName1).Value
        End If
    End If

    If AttributeValue1.Length > 0 Then
        MsgBox(AttributeValue1)
    Else
        MsgBox("No match found")
    End If
End Sub

答案 1 :(得分:0)

这可以通过加载xml doc,然后选择与某些条件匹配的特定节点来实现。 test[text()='some text'根据文本值和@信号选择节点,以选择所选节点的属性。您可以使用其他方法,例如选择与条件匹配的节点列表,然后循环它们。如果你想看到那种代码,请告诉我。

Dim doc As New Xml.XmlDocument()
doc.Load("c:/yourfile.XML")

Dim nValue = "en-US"
Dim xNodeAttr As Xml.XmlAttribute = doc.SelectSingleNode("/asr/item[text()='" & nValue & "' and //@qatest]")