|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I handle a NULL XmlElement?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
Show quote
Hide quote
"Paulers" <SuperG***@gmail.com> wrote in message What's happening is your source xml doesn't have a consistentnews:1167279074.753962.209980@79g2000cws.googlegroups.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 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. 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: Show quoteHide quote > "Paulers" <SuperG***@gmail.com> wrote in message > news:1167279074.753962.209980@79g2000cws.googlegroups.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.
When to use AndAlso vs And ?
Just a Program Trying to get started with tables... Beginner help with FolderBrowserDialog Get spawned process check if table is empty C# function be converted to VB.NET GotFocus versus PreviewKeyDown or Other VB.Net application deployment C# function be converted to VB.NET |
|||||||||||||||||||||||