Solved

VB.net - XML node, check attribute exists

Posted on 2006-11-21
6
4,539 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

809 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