Adam
Adam

Reputation: 6122

Select sibling node value based on multiple sibling conditions

I want to select the URL value (5.jpg) of the node that has Type=IMAGE and Key=XS ps: the order of the Images nodes is random, so I can't just select the nth node.

What I tried but fails:

Dim root As XmlNode = objXML.DocumentElement
Dim nodeList As XmlNodeList = root.SelectNodes("/Products/Product")

nodeList(i).SelectSingleNode("//URL[../Type='IMAGE' and ../Key='XS']").InnerText

and

nodeList(i).SelectSingleNode("/Images/[Type=IMAGE] and /Images/[Key=XS]").ChildNodes(0).SelectSingleNode("URL").InnerText

<Products>
    <Product>
        <Id>9200000093797005</Id>
        <Images>
            <Type>IMAGE</Type>
            <Key>XS</Key>
            <Url>5.jpg</Url>
        </Images>
        <Images>
            <Type>IMAGE</Type>
            <Key>S</Key>
            <Url>1.jpg</Url>
        </Images>
        <Images>
    </Product>
</Products>

Already checked here:

Upvotes: 0

Views: 724

Answers (1)

JayV
JayV

Reputation: 3271

You are almost correct in your attempts. I think the use of the XmlNodeList is what was causing your problems. If you switch to using an XmlNode and SelectSingleNode then you would've had the answer sooner.

    Dim root As XmlNode = objXml.DocumentElement
    Dim node As XmlNode = root.SelectSingleNode("//Products/Product/Images[Type='IMAGE' and Key='XS']/Url")
    Dim url As String = node.InnerText

Upvotes: 1

Related Questions