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.
There’s no problem opening two readers at the same time. However you cannot reuse
XmlDoc2after disposing it (through theusingblock).XmlReaderis forward-only, so basically you’d be running throughXmlDoc2for each iteration.If speed is your concern, you could try let
XmlDoc1be anXmlReader(as you’re running through it from top to bottom, once) and use one of the suggestedXmlDocumentorXDocumentclasses for the inner xml.