Solved

VB.net - XML node, check attribute exists

Posted on 2006-11-21
6
4,691 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
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:
The viewer will learn how to dynamically set the form action using jQuery.

707 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