Solved

VB.net - XML node, check attribute exists

Posted on 2006-11-21
6
4,578 Views
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


Thanks
0
Comment
Question by:rwallacej
6 Comments
 
LVL 10

Accepted Solution

by:
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
        Next
End If
0
 
LVL 7

Expert Comment

by:Dimandja
ID: 17989616
Use

result = element.hasAttribute(attName)

0
 

Author Comment

by:rwallacej
ID: 17990907
Dimandja
this is what I'm looking for rather than a for loop, but its a node I've got, not an element......
0
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

by:rwallacej
ID: 17990946
the node is from an XmlNodeList
0
 
LVL 6

Assisted Solution

by:manch
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
0
 

Author Comment

by:rwallacej
ID: 17998482
thanks
0

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