user416527
user416527

Reputation:

Nesting XmlReader

I have a need to search through an xml file, find a data set, examine a second xml file, check if there is related data, and then move on to the next data set.

Here's some sample code to illustrate:

XmlReader XmlDoc1 = XmlReader.Create("~/data/xml/myxml1.xml",settings);
XmlReader XmlDoc2= XmlReader.Create("~/data/xml/myxml2.xml",settings);

using (XmlDoc1) {
    XmlDoc1.Open();
    //get a data node
    using(XmlDoc2){
        XmlDoc2.Open();
        //find related information... if it's there
        XmlDoc2.Close();
   }
   //do stuff
   XmlDoc1.Close();
}

I'm pretty sure the above code would produce an error, but it would be too time consuming to read XmlDoc1, get a data set, close it, search XmlDoc2, close it... wash, rinse, repeat. So, I'm looking for a speedy way to accomplish the above.

Before you ask, I cannot run a DB on this site, so XML will have to suffice.

Upvotes: 0

Views: 219

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

There's no problem opening two readers at the same time. However you cannot reuse XmlDoc2 after disposing it (through the using block).

XmlReader is forward-only, so basically you'd be running through XmlDoc2 for each iteration.

If speed is your concern, you could try let XmlDoc1 be an XmlReader (as you're running through it from top to bottom, once) and use one of the suggested XmlDocument or XDocument classes for the inner xml.

Upvotes: 1

Related Questions