Null value while Reading XML file

KI-ChrisE

Member
Joined
Jun 19, 2005
Messages
5
Programming Experience
1-3
Null value question

I've written this code for my XML reader:
PHP:
NodeText = XmlNode.ChildNodes.Item(XmlChildNodeCount).Innertext

Now sometimes - when it reads this - it's going to come across nodes such as:

<field />

Obviously in that situation - there is no InnerText and as such it will generate an error:

Object referance not set to an instance of an object.

At least I presume that's what is causing the error since it works fine with nodes that contain innertext.

Anyhow, my question is - what can I do to continue to stop that error message but still have NodeText set as value of blank.

Thanks,

Chris
 
Hi,
Thanks for the reply.. I might of misunderstood but it still generates that error:

While XmlChildNodeCount <= XmlNode.ChildNodes.Count
NodeName = XmlNode.ChildNodes.Item(XmlChildNodeCount).Name

NodeText = XmlNode.ChildNodes.Item(XmlChildNodeCount).InnerText

XmlChildNodeCount = XmlChildNodeCount + 1

XmlTextBox.AppendText(NodeName &
": " & NodeText & "" & ControlChars.NewLine)

End While


Thanks,

Chris
 
from what i can see (in your code), i'm not sure what i am suppose to do in order to help you. Hmm ... you just said you have started from some examples that i've posted here ... but this is not that code (if i may).
Ok, let me get it str8. You have an idea and it should be realized. Well, in order to avoid duplication, confusing and wasting of time, please explain the scenario and we can go step by step through untill we resolve this problem.

Cheers ;)

btw, i suggested to add & "" righe after this line
NodeText = XmlNode.ChildNodes.Item(XmlChildNodeCount).InnerText & ""
but now i'm not sure why i adviced this ... it should read even empty nodes without problem ... however, follow the advice from the above and we gonna resolve confusing :)
 
Ok, ha, let's try again. Sorry I'm having difficulty explaining it myself you see.

Just so you know where I'm coming from:

VB.NET:
Imports System.IO
Imports System.Xml
 
Public Class XMLReader
	Public Sub New()
		' This call is required by the Windows Form Designer.
		InitializeComponent()
	End Sub
 
	Sub ReadXML()
		Try
			' Xml values
			Dim XmlDoc As XmlDocument
			Dim XmlNodeL As XmlNodeList
			Dim XmlNode As XmlNode
 
			' Additional xml values
			Dim XmlNodeCount As Integer = 0
			Dim XmlChildNodeCount As Integer = 0
			Dim XmlCurrent As Integer = 0
 
			' Node values
			Dim NodeName As String = ""
			Dim NodeText As String = ""
 
			' Setup and begin reading the XML
			XmlDoc = New XmlDocument
			XmlDoc.Load(Application.StartupPath & "\test.xml")
			XmlNodeL = XmlDoc.SelectNodes("/products/product")
 
			' Clear the text box of all values
			XmlTextBox.Clear()
 
			' Get the number of XML products
			XmlNodeCount = XmlNodeL.Count
 
			For Each XmlNode In XmlNodeL
				XmlCurrent = +1
				XmlProgressBar.Value = (XmlCurrent / XmlNodeCount) * 100
				XmlChildNodeCount = 0
 
				While XmlChildNodeCount <= XmlNode.ChildNodes.Count
					NodeName = XmlNode.ChildNodes.Item(XmlChildNodeCount).Name
					NodeText = XmlNode.ChildNodes.Item(XmlChildNodeCount).InnerText
 
					XmlChildNodeCount = XmlChildNodeCount + 1
 
					XmlTextBox.AppendText(NodeName & ": " & NodeText & ControlChars.NewLine)
				End While
			Next
		Catch ex As Exception
			MsgBox(ex.Message, MsgBoxStyle.Critical)
		End Try
	End Sub
 
	Private Sub goRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goRead.Click
		goRead.Visible = False
		XmlProgressBar.Visible = True
 
		ReadXML()
 
		XmlProgressBar.Visible = False
		goRead.Visible = True
	End Sub
End Class

That's the code.

And also the XML:

VB.NET:
<?xml version="1.0" encoding="UTF-8"?>
<products>
 <product>
  <TDProductId>8868924</TDProductId>
  <name>Techno Cat 3002</name>
  <description>Tom Wilson - Techno Cat 3002</description>
  <imageUrl>view/de/de/</imageUrl>
  <productUrl>prod=8868924</productUrl>
  <price>0.3</price>
  <currency>GBP</currency>
  <TDCategories>
   <TDCategory>
   <id>60</id>
   <name>Ringtones and logos</name>
   <merchantName>Polyphonic ringtone/Dance / Techno</merchantName>
   </TDCategory>
  </TDCategories>
  <fields/>
 </product>
 <product>
  <TDProductId>8870283</TDProductId>
  <name>Zodiac  Aries</name>
  <description>Zodiac Aries</description>
  <imageUrl>de/de/image_multijpeg/</imageUrl>
  <productUrl>prod=8870283</productUrl>
  <price>0.5</price>
  <currency>GBP</currency>
  <TDCategories><TDCategory>
   <id>60</id>
   <name>Ringtones and logos</name>
   <merchantName>Colour wallpaper/Nature - Astro &amp; Space</merchantName>
   </TDCategory>
  </TDCategories>
  <fields/>
 </product>
</products>

The place it gets stuck at is <fields />

So basically to explain - it's reading through the file. When it gets to <fields /> - because there's nothing there. It comes up with the error:

VB.NET:
Object reference not set to an instance of an object.

I need a way of saying - please continue anyway... if you see what I mean. :)

Thanks in advance,

Chris
 
Back
Top