How do I handle a NULL XmlElement?

P

Paulers

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
 
A

_AnonCoward

: 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
 
P

Paulers

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 said:
: 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
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top