Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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)

 

//"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.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 array's item with 0 accordingly?

 

Thanks,

Shurik.

Posted

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.

 

            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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...