2

I need to load a xml file in memory so I can access it several times from different forms. The xml is in this format:

  <Slides>
    <Slide>
      <Name>Name 1</Name>
      <Value>Value 1</Value>
      <Type>Type 1</Type>
    </Slide>
    <Slide>
      <Name>Name 2</Name>
      <Value>Value 2</Value>
      <Type>Type 2</Type>
    </Slide>
  </Slides>

I don't want to use a database to store the vars. Is there any other method to store the data in memory? Right now I'm using a txt file for each slide and streamreader, which I'm sure is not the best option.

EDIT: I added this code, but will I be able to get the slides every time without reading the xml file again?

        var slides = from s in XElement.Load("slides.xml").Elements("Slide")
                      select s;
        foreach (var slide in slides)
        {
            //code
        }
2
  • If the data are static this can help you: stackoverflow.com/a/2820420/600559 Commented Jun 10, 2012 at 12:40
  • 1
    Though there are exceptions, databases are typically used for persistent storage (on disk), not in memory. Commented Jun 10, 2012 at 12:40

3 Answers 3

5

You should use the XDocument Load method: http://msdn.microsoft.com/en-us/library/bb343181.aspx

i think it is better to use XDocument than the XmlDocument... but it depends on your requirements...

The XDocument is more memory optimized than the XmlDocument, and i think they both are easy for use (for getting values from the xml)

Sign up to request clarification or add additional context in comments.

3 Comments

and is the data stored in memory so I can use it whenever I want while the application is open ? or do I have to use the XElement.Load everytime I wanna get the data?
@KendallHildebrandt As long as you keep that instance of XDocument around, you only need to load it once.
Yea, you can do that, but i would save the whole XElement so i won't limit myself for something else - var xDocument = XDocument.Load("slides.xml");
1

See the XmlDocument class. The Load() method does what you want.

Comments

0

First you have to save the xml file in your project file or your hard disk.Then only you can load the xml file in code behind. You can create xml file dynamically.it will be much better.

using System.Xml;

    XmlDocument myxml = new XmlDocument();
    myxml.Load("D:/sample.xml");//Load you xml file from you disk what you want to load
    string element_1 = myxml.GetElementsByTagName("Name")[0].InnerText;
    string element_2 = myxml.GetElementsByTagName("Value")[0].InnerText; 
    string element_3 = myxml.GetElementsByTagName("Value")[0].InnerText;
    string element_4 = myxml.GetElementsByTagName("Name")[1].InnerText;
    string element_5 = myxml.GetElementsByTagName("Value")[1].InnerText; 
    string element_6 = myxml.GetElementsByTagName("Value")[1].InnerText;

Try this program it will help you.

2 Comments

I think this is a very dirty code especially that you haven't used a loop. What if I have 1000 records? shall I add [i] manually ?
I just posted for what you asked as question.If you need that kind of code, ask me i'll send you full code with loop...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.