[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with null records in xml in visual basic

Posted on 2009-02-17
2
Medium Priority
?
392 Views
Last Modified: 2012-05-06
I have e problem with some xml rows.

My xml like
<eventlist>
<event>
<eventnumber>1</eventnumber>
<eventtype>1</eventtype>
</event>
<event>
<eventnumber>2</eventnumber>
</event>
</eventlist>

I dont have eventtype ChildNodes in each row.. but I when I want to get this data I got a error.

When I want to read this xml with  selectnodes like..

m_nodelist = m_xmld.SelectNodes("//eventlist/event")
      
dim lig as xmlnode
 For Each lig In m_xmld.SelectNodes("//eventlist/event")

            console.write(lig("eventtype").InnerText)
next


then I dot a error like this

System.NullReferenceException: Object reference not set to an instance of an object. at ParsingUsingXmlDocument.Main()

how can I know null eventtype rows..
0
Comment
Question by:ftopcu
2 Comments
 
LVL 1

Accepted Solution

by:
sanginfotech earned 500 total points
ID: 23659146
Ur second 'event' row does not contain 'eventtype' element in it. Hence the error. Simply put an if condition to check 'eventtype' element exists in the node or not. Other way to do it is put whole "//eventlist/event/eventtype" in ur XPath query... Check the code below


'''******Using if condition
Dim objDocument As New XmlDocument
        objDocument.Load("C:\Documents and Settings\Administrator\Desktop\Test.xml")
        For Each objNode As XmlNode In objDocument.SelectNodes("//eventlist/event")
            If Not objNode("eventtype") Is Nothing Then
                Console.Write(objNode.Value)
            End If
        Next
 
 
'''******Using "//eventlist/event/eventtype" xpath
 
Dim objDocument As New XmlDocument
        objDocument.Load("C:\Documents and Settings\Administrator\Desktop\Test.xml")
        For Each objNode As XmlNode In objDocument.SelectNodes("//eventlist/event/eventtype")
            Console.Write(objNode.Value)
        Next

Open in new window

0
 

Author Comment

by:ftopcu
ID: 23659172
 If Not objNode("eventtype") Is Nothing Then
                Console.Write(objNode("eventtype").InnerText)
   End If


its solved my problem ..
Thanx
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question