Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if an XmlNode has a specific attribute?

Tags:

xml

vb.net

I would like to place an if condition within the sub that will tell it to run when the xml node STORE with attribute TEST="test.doc" does not exist. Any suggestions would be great. I'm new to vb.

Sub InsertNode(ByVal doc As XmlDocument)   
    Dim City As XmlNode = doc.DocumentElement

    Dim Location As XmlElement = doc.CreateElement("store")
    Location.SetAttribute("test", "test.doc")

    Dim books As XmlElement = doc.CreateElement("books")
    books.InnerXml = "New Words"
    Location.AppendChild(books)

    City.AppendChild(store)
End Sub 'InsertNode


Sample of the XML file
<city>
    <store test="test.doc">
        <books>
        "New Words" 
        </books>
    </store>
</city>
like image 231
MG. Avatar asked Mar 09 '09 16:03

MG.


2 Answers

Try something like that :

If Not doc.SelectSingleNode("//store[@test='test.doc']") Is Nothing Then
    Exit Sub
End If
like image 168
ybo Avatar answered Oct 04 '22 01:10

ybo


Assuming that Location is the node you want to check to see if your attribute exists:

If Location.Attributes.ItemOf("test") Is Nothing Then
    'Attribute doesnt exist
Else
    'Attribute does exist
End If
like image 21
Gavin Miller Avatar answered Oct 04 '22 01:10

Gavin Miller