473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I handle a NULL XmlElement?

Hello all,

I am trying to parse an XML document and populate objects to store in
an ArrayList but I am having issues when an element is NULL. I cant
seem to figure out how to handle it. I keep getting a null pointer
exception if a 'name' element is missing from the xml. Here is my code.
Any help is greatly appreciated.
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim menuFile As String = Application.StartupPath & "\" &
"vars.xml"
Dim doc As XmlDocument = New XmlDocument()

doc.Load(menuFile)

Dim myVars As XmlNodeList =
doc.DocumentElement.SelectNodes("/Root/Row")

Console.WriteLine(myVars.Count)
Dim element As XmlElement

For Each element In myVars
Dim type = element.ChildNodes.Item(0).InnerText
Dim variable = element.ChildNodes.Item(1).InnerText
Dim startByte = element.ChildNodes.Item(2).InnerText
Dim endByte = element.ChildNodes.Item(3).InnerText
Dim totalBytes = element.ChildNodes.Item(4).InnerText
Dim name = element.ChildNodes.Item(5).InnerText
Next
End Sub

Dec 28 '06 #1
2 2632

"Paulers" <Su*******@gmail.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
: Hello all,
:
: I am trying to parse an XML document and populate objects to store
: in an ArrayList but I am having issues when an element is NULL. I
: cant seem to figure out how to handle it. I keep getting a null
: pointer exception if a 'name' element is missing from the xml. Here
: is my code.
: Any help is greatly appreciated.
:
:
: Private Sub Form1_Load(ByVal sender As Object, ByVal e As
: System.EventArgs) Handles Me.Load
: Dim menuFile As String = Application.StartupPath & "\" &
: "vars.xml"
: Dim doc As XmlDocument = New XmlDocument()
:
: doc.Load(menuFile)
:
: Dim myVars As XmlNodeList =
: doc.DocumentElement.SelectNodes("/Root/Row")
:
: Console.WriteLine(myVars.Count)
: Dim element As XmlElement
:
: For Each element In myVars
: Dim type = element.ChildNodes.Item(0).InnerText
: Dim variable = element.ChildNodes.Item(1).InnerText
: Dim startByte = element.ChildNodes.Item(2).InnerText
: Dim endByte = element.ChildNodes.Item(3).InnerText
: Dim totalBytes = element.ChildNodes.Item(4).InnerText
: Dim name = element.ChildNodes.Item(5).InnerText
: Next
: End Sub
What's happening is your source xml doesn't have a consistent
structure. As a result, you code bombs when it attempts to read the
sixth child node of a given "Row" element. Here's one recommendataion
for dealing with this:

================================================== ===
[...]

For Each element In myVars
Dim type As String = GetValue(element, 0)
Dim variable As String = GetValue(element, 1)
Dim startByte As String = GetValue(element, 2)
Dim endByte As String = GetValue(element, 3)
Dim totalBytes As String = GetValue(element, 4)
Dim name As String = GetValue(element, 5)
Next

[...]

Private Function GetValue(ByVal element As XmlElement, _
ByVal ndx As Integer) As String
If ndx >= element.childNodes.count Then
Return ""
Else
Return element.ChildNodes.item(ndx).InnerText
End If
End Function
================================================== ===

By the way, I encourage you to use Option Strict unless you absolutely
can't and always declare your variable types.

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Dec 28 '06 #2
Ralf,

Thank you very much for your assistance. I appreciate the feedback and
will start using the strict option as you suggested. Also thank you for
taking the time to write that function, it helped me to better
understand how to handle null elements.

_AnonCoward wrote:
"Paulers" <Su*******@gmail.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
: Hello all,
:
: I am trying to parse an XML document and populate objects to store
: in an ArrayList but I am having issues when an element is NULL. I
: cant seem to figure out how to handle it. I keep getting a null
: pointer exception if a 'name' element is missing from the xml. Here
: is my code.
: Any help is greatly appreciated.
:
:
: Private Sub Form1_Load(ByVal sender As Object, ByVal e As
: System.EventArgs) Handles Me.Load
: Dim menuFile As String = Application.StartupPath & "\" &
: "vars.xml"
: Dim doc As XmlDocument = New XmlDocument()
:
: doc.Load(menuFile)
:
: Dim myVars As XmlNodeList =
: doc.DocumentElement.SelectNodes("/Root/Row")
:
: Console.WriteLine(myVars.Count)
: Dim element As XmlElement
:
: For Each element In myVars
: Dim type = element.ChildNodes.Item(0).InnerText
: Dim variable = element.ChildNodes.Item(1).InnerText
: Dim startByte = element.ChildNodes.Item(2).InnerText
: Dim endByte = element.ChildNodes.Item(3).InnerText
: Dim totalBytes = element.ChildNodes.Item(4).InnerText
: Dim name = element.ChildNodes.Item(5).InnerText
: Next
: End Sub
What's happening is your source xml doesn't have a consistent
structure. As a result, you code bombs when it attempts to read the
sixth child node of a given "Row" element. Here's one recommendataion
for dealing with this:

================================================== ===
[...]

For Each element In myVars
Dim type As String = GetValue(element, 0)
Dim variable As String = GetValue(element, 1)
Dim startByte As String = GetValue(element, 2)
Dim endByte As String = GetValue(element, 3)
Dim totalBytes As String = GetValue(element, 4)
Dim name As String = GetValue(element, 5)
Next

[...]

Private Function GetValue(ByVal element As XmlElement, _
ByVal ndx As Integer) As String
If ndx >= element.childNodes.count Then
Return ""
Else
Return element.ChildNodes.item(ndx).InnerText
End If
End Function
================================================== ===

By the way, I encourage you to use Option Strict unless you absolutely
can't and always declare your variable types.

Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Dec 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Timothy V | last post by:
Hi, I have and XmlElement from within an XmlDocument. I wish to RemoveAll() from within the XmlElement plus itself. For example: <Notes> <Note id="n1"> <Info>A message</Info> </Note> <Note...
4
by: Steven.Xu | last post by:
Hello everyone. I have two instance of xml.xmldocument. One is XA another is XB. XA has a child node XA1. XA1 has many attributes and child nodes. Now I want to copy XA1 to XB with all of it's...
3
by: Robb Gilmore | last post by:
Hello, We have a C#.NET app which is calling a Java webservice. We use the wsdl file exportted from the java webservice to create our web-reference in Visual Studio. We are able to create the...
1
by: andrej | last post by:
hi, ich habe eine anwendung, welche ein xml document erstellt. um festzustellen, ob ein element bereits vorhanden ist, verwende ich die funktion selectsinglenode( ....) diese funktion...
6
by: Markus Eßmayr | last post by:
Hello, I'd like to consume a WebService, which returns an array of objects which include several members of type System.String, System.Decimal and System.DateTime. In the WSDL-file, the members...
3
by: Frank Perry | last post by:
Howdy, I'm trying to write data out the com port. I have taken the code from the sample on the MSDN Library CD and used the parts that seem relevant. I can open the com port with CreateFile...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
0
by: Kathy Burke | last post by:
I'm providing the following syntax in hopes someone could tell me why I get an object reference error on the second one. The first one works, using an xmlDocument, the second one immediately...
1
by: funkymp | last post by:
im trying to create a user interface that allows a user to upload there own mixes which are then added to a itunes podcast. basically, my problem is. when it updates the podcast.xml file, i need...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.