13

I want to save and load my xml data using XmlReader. But I don't know how to use this class. Can you give me a sample code for start?

Ramesh Soni
  • 15,867
  • 28
  • 93
  • 113
mohammad reza
  • 3,292
  • 6
  • 29
  • 39

4 Answers4

12

MSDN has a simple example to get you started here.

If you're interested in reading and writing XML documents, and not just specifically using the XmlReader class, there's a nice article covering a few of your options here.

But if you just want to get started and play around, try this:

 XmlReaderSettings settings = new XmlReaderSettings();
 settings.IgnoreWhitespace = true;
 settings.IgnoreComments = true;
 XmlReader reader = XmlReader.Create("file.xml", settings);
Matt Brindley
  • 9,739
  • 7
  • 47
  • 51
  • It always surprises me when I find little snippets of code that solve my issue and they are snippets that I couldnt find *anywhere* including the MS site. Thanks, this one saved me. – Unknown Coder Jan 20 '13 at 06:01
9

Personally I have switched away from XMLReader to System.XML.Linq.XDocument to manage my XML data files. This way I can easily pull data from xml into objects and manage them like any other object in my program. When I am done manipulating them I can just save the changes back out the the xml file at any time.

        //Load my xml document
        XDocument myData = XDocument.Load(PhysicalApplicationPath + "/Data.xml");

        //Create my new object
        HelpItem newitem = new HelpItem();
        newitem.Answer = answer;
        newitem.Question = question;
        newitem.Category = category;

        //Find the Parent Node and then add the new item to it.
        XElement helpItems = myData.Descendants("HelpItems").First();
        helpItems.Add(newitem.XmlHelpItem());

        //then save it back out to the file system
        myData.Save(PhysicalApplicationPath + "/Data.xml");

If I want to use this data in an easily managed data set I can bind it to a list of my objects.

        List<HelpItem> helpitems = (from helpitem in myData.Descendants("HelpItem")
                  select new HelpItem
                  {
                       Category = helpitem.Element("Category").Value,
                       Question = helpitem.Element("Question").Value,
                       Answer = helpitem.Element("Answer").Value,
                  }).ToList<HelpItem>();

Now it can be passed around and manipulated with any inherent functions of my object class.

For convenience my class has a function to create itself as an xml node.

public XElement XmlHelpItem()
    {
        XElement helpitem = new XElement("HelpItem");
        XElement category = new XElement("Category", Category);
        XElement question = new XElement("Question", Question);
        XElement answer = new XElement("Answer", Answer);
        helpitem.Add(category);
        helpitem.Add(question);
        helpitem.Add(answer);
        return helpitem;
    }
benjamin
  • 1,087
  • 7
  • 10
  • 4
    If I need to query XML like I'm querying other data I agree. But, for simple validation or forward-only searching XmlReader is the better choice because it is more efficient. – Josh Jul 14 '11 at 15:34
  • 14
    We have a serious problem when a question titled "How to use XmlReader class" has an accepted answer that doesn't come close to answering the question. I'm voting to remove or rename the question. – Brian Warshaw Jun 13 '12 at 17:45
  • 5
    This isn't a good solution if you are reading huge XML documents. XMLReader allows you to stream it and read in pieces. – richard Dec 05 '12 at 04:27
7

You should use the Create method instead of using new, since XmlReader is an abstract class using the Factory pattern.

var xmlReader = XmlReader.Create("xmlfile.xml");
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
6

From the excellent C# 3.0 in a Nutshell, consider looking at the sample code from chapter 11.

jason
  • 236,483
  • 35
  • 423
  • 525