Solved

VB.net - XML node, check attribute exists

Posted on 2006-11-21
6
4,617 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
Technology Partners: 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!

 

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

Technology Partners: 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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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