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
        }

3 Answers3

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)

eyossi
  • 4,230
  • 22
  • 20
  • 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? – Kendall Hildebrandt Jun 10 '12 at 13:15
  • 1
    @KendallHildebrandt As long as you keep that instance of `XDocument` around, you only need to load it once. – vcsjones Jun 10 '12 at 13:27
  • 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"); – eyossi Jun 10 '12 at 14:54
1

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

Filip
  • 2,285
  • 1
  • 19
  • 24
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.

Community
  • 1
  • 1
CHANDRA
  • 4,778
  • 8
  • 32
  • 51
  • 1
    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 ? – Kendall Hildebrandt Jun 10 '12 at 13:55
  • 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... – CHANDRA Jun 10 '12 at 16:48