Home All Groups Group Topic Archive Search About

checking for element in xml document

Author
18 Apr 2006 1:00 PM
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?

Author
18 Apr 2006 1:19 PM
zacks
If act_nbr is Nothing Then
....
Author
18 Apr 2006 1:53 PM
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.


za***@construction-imaging.com wrote:
Show quoteHide quote
> If act_nbr is Nothing Then
> ...
>
Author
18 Apr 2006 2:43 PM
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.
Author
18 Apr 2006 2:26 PM
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
--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com



Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:%23UT5GguYGHA.3832@TK2MSFTNGP04.phx.gbl...
> 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?
Author
18 Apr 2006 2:29 PM
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

--
Get a powerful web, database, application, and email hosting with KJM
Solutions
http://www.kjmsolutions.com



Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:%23UT5GguYGHA.3832@TK2MSFTNGP04.phx.gbl...
> 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?