Solved - XML node, check attribute exists

Posted on 2006-11-21
Last Modified: 2013-11-19
How do I check if an attribute exists in xml node

This, as expected, throws a null pointer.....but checking if the attribute exists in first place is surely better than catching exceptions?

Dim node As XmlNode = whatever
Dim x as String
x = node.Attributes.GetNamedItem("fieldDoesntExist").Value

Question by:rwallacej
LVL 10

Accepted Solution

tpwells earned 250 total points
ID: 17989573
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
End If

Expert Comment

ID: 17989616

result = element.hasAttribute(attName)


Author Comment

ID: 17990907
this is what I'm looking for rather than a for loop, but its a node I've got, not an element......
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 17990946
the node is from an XmlNodeList

Assisted Solution

manch earned 250 total points
ID: 17994147
Use the Function :

    Public Function GetSectionValue(ByVal sectionName As String, ByVal key As String) As String

        Dim mobjXMLDoc As XmlDocument = Nothing

        mobjXMLDoc = New XmlDocument

        mobjXMLDoc.Load(write ur XML File Name Here)

        Dim psXPathCriteria As String = "//" & sectionName
        Dim pobjSectionNode As XmlNode = mobjXMLDoc.SelectSingleNode(psXPathCriteria)

        If pobjSectionNode Is Nothing = False Then
            psXPathCriteria = "//add[@key='" & key & "']"
            Dim pobjChildNode As XmlNode = pobjSectionNode.SelectSingleNode(psXPathCriteria)
            If pobjChildNode Is Nothing = False Then
                Return pobjChildNode.Attributes("value").Value
            End If
        End If
        Return Nothing
    End Function

(u have to provide the Section name and the Key Name to get the Key Value of that particular Section's Key)

Try It

Author Comment

ID: 17998482

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question