checking for element in xml document

C

cj

How would I check to see if a element is in an xml document.

Here's the document:

test.xml
<?xml version="1.0" encoding="UTF-8"?>
<request>
<key>5678</key>
<type>0200</type>
<transaction>01</transaction>
<response_fields>
<code>045</code>
<auth/>
<otc>X</otc>
</response_fields>
</request>

Here's my code:

Dim doc As New Xml.XmlDocument
doc.Load("c:\test.xml")
Dim key As Xml.XmlNodeList = doc.GetElementsByTagName("key")
Dim otc As Xml.XmlNodeList = doc.GetElementsByTagName("otc")
Dim auth As Xml.XmlNodeList = doc.GetElementsByTagName("auth")
Dim act_nbr As Xml.XmlNodeList = doc.GetElementsByTagName("act_nbr")

Now for the problem, act_nbr doesn't exist in this file so the following
line throws and error.

MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ", " &
auth(0).InnerText & ", " & act_nbr(0).InnerText)

I get "Object reference not set to an instance of an object." which is
understandable since "act_nbr" doesn't exist in this xml document. How
do I test to see if it's in there before trying to read it?
 
A

_AnonCoward

:
: How would I check to see if a element is in an xml document.
:
: Here's the document:
:
: test.xml
: <?xml version="1.0" encoding="UTF-8"?>
: <request>
: <key>5678</key>
: <type>0200</type>
: <transaction>01</transaction>
: <response_fields>
: <code>045</code>
: <auth/>
: <otc>X</otc>
: </response_fields>
: </request>
:
: Here's my code:
:
: Dim doc As New Xml.XmlDocument
: doc.Load("c:\test.xml")
: Dim key As Xml.XmlNodeList = doc.GetElementsByTagName("key")
: Dim otc As Xml.XmlNodeList = doc.GetElementsByTagName("otc")
: Dim auth As Xml.XmlNodeList = doc.GetElementsByTagName("auth")
: Dim act_nbr As Xml.XmlNodeList = doc.GetElementsByTagName("act_nbr")
:
: Now for the problem, act_nbr doesn't exist in this file so the following
: line throws and error.
:
: MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ", " &
: auth(0).InnerText & ", " & act_nbr(0).InnerText)
:
: I get "Object reference not set to an instance of an object." which is
: understandable since "act_nbr" doesn't exist in this xml document. How
: do I test to see if it's in there before trying to read it?


There are a number of ways. They consist of testing to see if an object is
not null before accessing it. Some approaches


'************************************************
Dim sKey As String
Dim sOtc As String
Dim sAuth As String
Dim sAct_nbr As String

Dim doc As New Xml.XmlDocument
doc.Load("c:\test.xml")
Dim key As Xml.XmlNodeList = doc.GetElementsByTagName("key")
Dim otc As Xml.XmlNodeList = doc.GetElementsByTagName("otc")
Dim auth As Xml.XmlNodeList = doc.GetElementsByTagName("auth")
Dim act_nbr As Xml.XmlNodeList = doc.GetElementsByTagName("act_nbr")

If Not key(0) Is Nothing Then sKey = key(0).InnerText
If Not otc(0) Is Nothing Then sOtc = otc(0).InnerText
If Not auth(0) Is Nothing Then sAuth = auth(0).InnerText
If Not act_nbr(0) Is Nothing Then sAct_nbr = act_nbr(0).InnerText

MessageBox.Show(sKey & ", " & sOtc & ", " & sAuth & ", " & sAct_nbr)

'************************************************


Or, use a function call


'************************************************
Dim doc As New xmlDocument
doc.Load("test.xml")
Dim key As XmlNodeList = doc.GetElementsByTagName("key")
Dim otc As XmlNodeList = doc.GetElementsByTagName("otc")
Dim auth As XmlNodeList = doc.GetElementsByTagName("auth")
Dim act_nbr As XmlNodeList = doc.GetElementsByTagName("act_nbr")

MessageBox.Show( _
GetText(key) & ", " & _
GetText(otc) & ", " & _
GetText(auth) & ", " & _
GetText(act_nbr))

'[...]

Private Function GetText(NodeList As XMLNodeList) As String
If NodeList(0) Is Nothing Then
Return ""
Else
Return NodeList(0).InnerText
End If
End Function
'************************************************

Ralf
 
C

cj

Nope. act_nbr is something. However you have sparked an idea.

If act_nbr.Count = 0 Then
....

Why didn't that occur to me earlier? Oh well, Thanks for getting my
mind unstuck.
 
V

vbnetdev

If act_nbr Is Nothing Then
MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ",
" & auth(0).InnerText & ", ")
Else
MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ",
" & auth(0).InnerText & ", " & act_nbr(0).InnerText)
End If
 
V

vbnetdev

Sorry my mistake

If act_nbr.Item(0) Is Nothing Then
MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ",
" & auth(0).InnerText & ", ")
Else
MessageBox.Show(key(0).InnerText & ", " & otc(0).InnerText & ",
" & auth(0).InnerText & ", " & act_nbr(0).InnerText)
End If
 
Z

zacks

Ahh, yes, I missed it that get were getting a list of elements back.
Same scenario with the GetNodes method. Check the Count property for
zero. For a GetElementByID or GetNode method, you would have to check
the result to nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top