Link to home
Start Free TrialLog in
Avatar of Mike_Stevens
Mike_StevensFlag for United States of America

asked on

Determine if XML node exists using VB.NET

I have an existing XML file that i need to write values to.  How can I determine if a specific node already exists in the file?   If the node does not exist how can i create just one specific node with affecting the entire xml document?
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

you can loop through the attributes on the node if there are attributes present

If (yourNode.Attributes.Count>0) Then
       For atrib As Integer = 0 To yourNode.Attributes.Count
            If (yourNode.Attributes(atrib).Name = "FieldName") Then
                'found it
                x = yourNode.Attributes.GetNamedItem("fieldDoesntExist").Value
                'or  x = yourNode.Attributes(atrib).Value
                Exit For ' to exit this for if only needed to find this one attribute
            End If
        Next
End If

Answer from expert tpwells found at https://www.experts-exchange.com/questions/22068480/VB-net-XML-node-check-attribute-exists.html
ASKER CERTIFIED SOLUTION
Avatar of barrislb
barrislb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial