CKR
CKR

Reputation: 43

How to use XmlReader to read data in a List Part 2

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.

Upvotes: 2

Views: 215

Answers (2)

Anders Arpi
Anders Arpi

Reputation: 8417

Are you doing this in a server environment, like with an ASP.NET application? That might introduce some more steps, but the general idea is to use a static object to hold the XML Document that you load into memory. Much like you have done. And that's really all there is to it. Could you provide some more context?

You also don't really need to use the XML reader (unless you only want that sort of forward only access);

public static class Globals
{
    public static XDocument ConfigFile = XDocument.Load("C:/somefile.xml");
}


public void SomeOtherFunctionSomewhere()
{
    var configName = Globals.ConfigFile
        .Descendants("someconfigsection")
        .Descendants("configName")
        .First().Value;
}

If you are storing your program's configuration in an XML file - consider reading the entire configuration into an in-memory object that you create yourself at startup. This will be an even faster way to access the config sections during runtime than having to query the XML every time (even if the XML is already in memory). Contrived example below:

public class Configuration
{
    //just some made up config settings
    public string Name {get;set;}
    public int Id {get;set;}
    public int XDimension {get;set;}
    public int YDimension {get;set;}
}

class Program
{
    static Configuration Config;
    static void Main(string[] args)
    {
        Config = ReadConfig();
    }

    private static Configuration ReadConfig()
    {
        var config = new Configuration();
        //read entire XML and set properties on the config object
        return config;
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502446

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 separate XmlReader each time you call it.)

From the docs for XmlReader:

Represents a reader that provides fast, non-cached, forward-only access to XML data.

(Emphasis mine.) I don't see anything around resetting, and I wouldn't expect to.

Given that you've got the information in an XDocument to start with, I would personally try to do all the querying with that rather than using the rather-harder-to-work-with XmlReader anyway, 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.

Upvotes: 1

Related Questions