|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
checking for element in xml documentHere'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? 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 > ... > 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. 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 -- Show quoteHide quoteGet a powerful web, database, application, and email hosting with KJM Solutions http://www.kjmsolutions.com "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? 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 -- Show quoteHide quoteGet a powerful web, database, application, and email hosting with KJM Solutions http://www.kjmsolutions.com "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?
CreateObject internet access
Weather board, how to get html data. Reference different versions of an ActiveX-dll Easy Q: 0.0 --> Output as string? Get table names from MSAccess Datasource Free unzip plug-in for VB.NET? Byte to int Conversion ..?? Dropdownlist PNG control graphic abnomality Store Item Template with Project |
|||||||||||||||||||||||