0

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.

1 Answers1

1

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.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • While noodling this through, I was starting to think the same thing... so that's something I didn't know, you can only open and close a Reader (SQL or otherwise) once? That's probably been the source of the error that I've seen before. I'll give this a try. –  Nov 21 '12 at 20:22
  • For `IDataReader`s (SQL readers) you can usually only have one open per connection at the same time. An `XmlReader` should not care about other `XmlReader`s (unless they share the same data source, but in your case there are two different files). – C.Evenhuis Nov 21 '12 at 22:08