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?
相关问题
- Sorting 3 numbers without branching [closed]
- Illegal to have multiple roots (start tag in epilo
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
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:
You should use the
Create
method instead of usingnew
, sinceXmlReader
is anabstract class
using the Factory pattern.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.
If I want to use this data in an easily managed data set I can bind it to a list of my objects.
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.
From the excellent C# 3.0 in a Nutshell, consider looking at the sample code from chapter 11.