detecting missing XML elements (C#)

Shurik12

Member
Joined
Jul 10, 2002
Messages
22
Location
Belgium
Hello,

Id like to read in a XML file elements content into a number of arrays

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

Code:
//"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."


Ive tried something similar too

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

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

                {
                        Spread6mArray[i] = System.Convert.ToString(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 arrays item with 0 accordingly?

Thanks,
Shurik.
 
Avoid GetElementByTagName

I think you need to try a different approach. Your current method makes it impossible to know which rows are missing a Spread6m element, since the GetElementByTagName places all elements in an array regardless of their position in the document, making it not obvious which row they are children of.

I would recommend you loop through the row elements one at a time and process each individually.

C#:
            XmlNodeList rows;
            XmlNode     node;

            //Select rows
            rows = doc.SelectNodes("row");
            
            //Initialize arrays
            gridIDArray = new int[rows.Count];
            spread6mArray = new string[rows.Count];

            for (int i = 0; i < rows.Count; i++) {
                //Process row

                //Get GridID
                node = rows[i].SelectSingleNode("GridID");
                gridIDArray[i] = (node == null) ? 0 : int.Parse(node.InnerText);

                //Get Spread6m
                node = rows[i].SelectSingleNode("Spread6m");
                spread6mArray[i] = (node == null) ? "0" : node.InnerText;

                //etc
            }

There are of course other methods you can use, such as creating an XSD and using it to instantiate a graph of objects with specified default values.

Also, you can mark up C# code with [cs][/cs].

Good luck :cool:
 
Hi,

Thanks for the response.
Youre absoultely right, the is the approach to take.

Thanks for the help

Shurik.

p.s the hint about the tags is taken.
 
Back
Top