How to check if Attribute exists in Child Node

How can i access the type Attributes directly if my XmlNode holds the below xml file.

i tried the following with no luck
dim elementPath as String = "ns:RequestedAddr/ns:type"
dim elementPath as String = "ns:RequestedAddr/type"
dim elementPath as String = "RequestedAddr/type"

Dim myNode As XmlNode = node.Attributes(elementPath)

I need to check if the node / Attribute exists to avoid null exceptions


<RoutingInfo>
        <RequestedAddr type="unknown">12099925140</RequestedAddr>
        <DestAddr type="e164">+12099925140</DestAddr>
        <RoutedAddr type="national">2099925140</RoutedAddr>
        <CallingPartyRoutedAddr type="national">2099313552</CallingPartyRoutedAddr>
        <CallingPartyOrigAddr type="national">2099313552</CallingPartyOrigAddr>
    </RoutingInfo>

Open in new window

Confused CoderAsked:
Who is Participating?
 
zc2Commented:
What XML element the "node" object refers to? Is it "RoutingInfo" or one of its children?
If it is "RoutingInfo", then you first get a reference to a child, like
node.SelectSingleNode("ns:RequestedAddr")
and then use the GetAttribute() method:
Set RequestedAddr = node.SelectSingleNode("ns:RequestedAddr")
type_string = RequestedAddr.GetAttribute("ns:type")

Open in new window

I am not sure, will the following work or not, but you could try
Set RequestedAddr_type_attr = node.SelectSingleNode("ns:RequestedAddr/@ns:type")
if not RequestedAddr_type_attr is Nothing then type_string = RequestedAddr_type_attr.Value

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.