Shurik

Shurik

  • NA
  • 1
  • 0

detecting missing XML elements

Nov 29 2006 11:05 AM

Hello,

I'd like to read in a XML file elements' content into a number of arrays

(if it makes any difference: I'm using PL-SQL associative arrays to make
a "bulk" insert of the XML file into an Oracle table)

The code I'm currently using


//"load" the xml file
XmlTextReader textReader = New XmlTextReader(path);
textReader.Read();
XmlDocument doc = New XmlDocument();
doc.Load(textReader);

// Get all elements here
XmlNodeList gridid = doc.GetElementsByTagName("gridid");
XmlNodeList spread6m = doc.GetElementsByTagName("Spread6m");
.... other elements go here


// Loop throught the whole document And populate the arrays (declared And instantiated alerady)
For (int i = 0; i < CountElements; i++) 

{
GridIDArray[i] = System.Convert.ToInt32(gridid[i].InnerXml);
Spread6mArray[i] = System.Convert.ToString(spread6m[i].InnerXml);  
}
...

Since 'spread6m' element is not present in each of the root elements (see the attachment for a screenshoot), application throws "Object reference not set to an instance of an object."


I've tried something similar too


// Get a XML node Type object.
Type XmlNodeType = TypeOf(XmlNode);

  If (XmlNodeType.IsInstanceOfType(spread6m[i]))

                {
                        Spread6mArray[i] = System.Convert.ToDouble(spread6m[i].InnerXml);
                }
                    Else
               
                {
                        Spread6mArray[i] = 0;
                }

but it did not really seem to help


So the question is: how can I detect that an element is missing within the current root element and populate the array's item with 0 accordingly?

Thanks,
Shurik.