C# 如何使用 XmlReader 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/904129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:27:11  来源:igfitidea点击:

How to use XmlReader class?

c#xmlxmlreader

提问by mohammad reza

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?

我想使用 XmlReader 保存和加载我的 xml 数据。但我不知道如何使用这个类。你能给我一个开始的示例代码吗?

采纳答案by benjamin

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.

我个人已经从 XMLReader 切换到 System.XML.Linq.XDocument 来管理我的 XML 数据文件。通过这种方式,我可以轻松地将数据从 xml 提取到对象中,并像程序中的任何其他对象一样管理它们。当我完成对它们的操作后,我可以随时将更改保存回 xml 文件。

        //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.

为方便起见,我的班级有一个将自身创建为 xml 节点的功能。

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;
    }

回答by Mehrdad Afshari

You should use the Createmethod instead of using new, since XmlReaderis an abstract classusing the Factory pattern.

您应该使用Create方法而不是使用new,因为XmlReader它是abstract class使用工厂模式

var xmlReader = XmlReader.Create("xmlfile.xml");

回答by jason

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

从优秀的C# 3.0 in a Nutshell 中,考虑查看第 11 章中的示例代码

回答by Matt Brindley

MSDN has a simple example to get you started here.

MSDN 有一个简单的示例可以帮助您从这里开始。

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.

如果您对阅读和编写 XML 文档感兴趣,而不仅仅是专门使用 XmlReader 类,这里有一篇很好的文章,介绍了您的一些选项

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);