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?
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?