Groups | Blog | Home
all groups > dotnet xml > july 2007 >

dotnet xml : How to deal with empty element using ReadXML


Bill Nguyen
7/12/2007 1:41:59 PM
below is a very simple XML file.
I use ReadXml to read data.
When it reaches an empty element as <Product /> below, my program crashes.
The error:
System.NullReferenceException: Object reference not set to an instance of an
object.

How can I assign a value to this empty element?

Thanks

Bill

---------------------

- <Inventory Type="Physical">
<Product />
<TankNumber>0031022207-1</TankNumber>
<UserDefinedTag>0</UserDefinedTag>
<DateTime>2007-07-11T03:52:00</DateTime>
<Volume>0</Volume>
<Ullage>1005</Ullage>
<Height>0</Height>
<WaterHeight>0</WaterHeight>
<DaystoEmpty>0</DaystoEmpty>
</Inventory>

Bjoern Hoehrmann
7/13/2007 12:35:49 AM
* Bill Nguyen wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

That would be the wrong solution for the problem. You have to rewrite
your code so that the exception does not occur. Some part of your code
assumes all elements (or the Product element specifically) have some
children, you need to remove that assumption.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
AddThis Social Bookmark Button