2

I've been searching SO (and the rest of the Internet) for the answer, but I can't seem to find a solution for selecting an XML node based on an attribute.

Example XML:

<foo>
    <bar name="do">A</bar>
    <bar name="rae">B</bar>
    <bar name ="mi">C</bar>
</foo>

So if I want to get B by the rae value of the name attribute, I've tried:

myValue = myXML.selectSingleNode("//foo/bar/").Attributes.getNamedItem("rae").Text

Thanks!

4
  • Check out this Link if you haven't before. Commented Aug 5, 2013 at 17:37
  • I did come across that, but it does not answer my specific question since it uses a For Each to iterate through the items. I'm hoping to pull a value from a specific node before I start any further parsing. Commented Aug 5, 2013 at 17:52
  • Then you can leave off the For Each loop? Instead of looping through the children, grab a node by its name, then grab the Attributes, then the named item. The code within the For Each loop should still be applicable. Commented Aug 5, 2013 at 18:10
  • If you look at the example I provided it is essentially all of the code within the for loop. The problem is I can't retrieve it based on name without a loop because there are multiple nodes with that name. Commented Aug 5, 2013 at 18:20

3 Answers 3

3

The solution that ended up working for me is:

myValue = myXML.selectSingleNode("var[@name='ID']").Text
Sign up to request clarification or add additional context in comments.

3 Comments

That's the correct way to do it. However be aware that this will fail with a run-time error when selectSingleNode turns up Nothing. Better is Set node = myXML.selectSingleNode("var[@name='ID']") and a check If Not node Is Nothing Then ....
I will have to implement that at some point. My current, albeit terrible, way of selecting is to only grab the names of fields I know exist in my data set. Thanks for the suggestion!
You can never be sure something exists. ;-)
1

If there are multiple nodes, then I don't think you'd be able to use SelectSingleNode. Instead, you can use XPath and getElementsByTagName. In addition, (though it may not be necessary in your case) to ensure that you're only getting one node, you can use NextNode. The final code using your sample XML would be...

myValue = myXML.getElementsByTagName("/foo[@name='rae']/bar").nextNode.nodeTypedValue

1 Comment

selectSingleNode ended up working for me. Much thanks for your input!
1

Using your example...

myValue = myXML.SelectSingleNode("/foo/bar[@name='rae']").InnerXML

Note the use of the @ in the variable name. Your criteria (everything between the square brackets) goes on the level that you are searching through.

3 Comments

Your solution did not work for me but it ended up being extremely close to the working solution. Please see my post. Much thanks for steering me in the right direction!
Also, it doesn't seem that the bracketed criteria searches a level higher.
You're right. The square brackets go on the bar element. Fixed code above. I was testing using an XML file that has the tag at a higher level, so I got confused ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.