Okay, it appeared that I had an answer for this question, but I was wrong. Probably because I asked the wrong thing. Here is what I want to do:
I want to take an xml document and pull it into memory. Then, at will, I want to run queries on that document and get information out of it. I want to do this whenever I need data WITHOUT loading the document into memory again. After all, the point is to stop hitting up the disk when I need the data. The methods I’ve been using only work ONCE, and I need to do this multiple times. Here is the code that I tried:
public static class GrabFile
{
public static XDocument configData = XDocument.Load(@"myxml.xml");
public static XmlReader templateReader = configData.CreateReader();
}
I thought this would copy the document into memory, and I’d be able to use the templateReader to query the configData any time I wanted to by using this:
while (GrabFile.templateReader.Reader())
{
//get the data I wanted
}
I thought I could put that while statement, and create others to do specific queries in a method, and call them whenever I needed the info. But it only works ONCE.
As I said, I’m not overly familiar with this, so there’s probably a way to do it that’s easy.
Also, people in the other thread wanted an example of what was in the xml document. That’s irrelevant. The point is I want to put the document into memory, then query it as many times as needed with out accessing it from the disk and creating another reader. And yes, I want to use a reader for this.
Perhaps I need to move the pointer in the file in memory back to the top so it’ll read it again?
Thanks for any help.
Why do you not want to create another reader?
Just calling
GrabFile.configData.CreateReader()each time you need it is the simplest approach. (That won’t load it from disk again, but it will create a separateXmlReadereach time you call it.)From the docs for
XmlReader:(Emphasis mine.) I don’t see anything around resetting, and I wouldn’t expect to.
Given that you’ve got the information in an
XDocumentto start with, I would personally try to do all the querying with that rather than using the rather-harder-to-work-withXmlReaderanyway, but obviously that’s your call. It would help if you’d give some justification though – as you say you’re “not overly familiar with this”, so it’s worth revisiting your assumptions about how you’re tackling whatever the higher-level task is.