简体   繁体   English

如何让XmlReader读取C#中的XML数据

[英]How can I get XmlReader to read my XML data in C#

I am using the following Microsoft sample code to read XML: 我正在使用以下Microsoft示例代码来读取XML:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            StringBuilder output = new StringBuilder();

            String xmlString =
                    @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
        <Items>
          <Item>test with a child element <more/> stuff</Item>
        </Items>";
            // Create an XmlReader
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                XmlWriterSettings ws = new XmlWriterSettings();
                ws.Indent = true;
                using (XmlWriter writer = XmlWriter.Create(output, ws))
                {

                    // Parse the file and display each of the nodes.
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                writer.WriteStartElement(reader.Name);
                                break;
                            case XmlNodeType.Text:
                                writer.WriteString(reader.Value);
                                break;
                            case XmlNodeType.XmlDeclaration:
                            case XmlNodeType.ProcessingInstruction:
                                writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                break;
                            case XmlNodeType.Comment:
                                writer.WriteComment(reader.Value);
                                break;
                            case XmlNodeType.EndElement:
                                writer.WriteFullEndElement();
                                break;
                        }
                    }

                }
            }
            var OutputTextBlock.Text = output.ToString();
        }
    }
}

But it is giving me an error saying OutputTextBlock does not exist. 但这给我一个错误,指出OutputTextBlock不存在。

I tried to put a var before this but still get an error. 我试图在此之前放置一个var,但仍然出现错误。 Saying implicitely typed variables must be initialized. 说隐式类型的变量必须被初始化。 Can someone tell me what I am doing wrong. 有人可以告诉我我在做什么错。

Is there an easier way that I can be doing this? 有没有更简单的方法可以做到这一点?

I posted this question: How can I extract one element of data out of an XML file on my Windows desktop? 我发布了这个问题: 如何从Windows桌面上的XML文件中提取一个数据元素?

But the answer was just to use XmlReader. 但是答案只是使用XmlReader。 I am not really sure where to start. 我不太确定从哪里开始。 I would like to mark the other question as answered but nobody replied with an answer. 我想将另一个问题标记为已回答,但没有人回答。

The problem in your code has nothing to do with XML - it's just the sample code which assumes there is a variable called OutputTextBlock . 在你的代码的问题无关,与XML -它只是它假定一个叫做变量的代码示例OutputTextBlock That suggests the demo code was written in the context of a WPF app rather than a console app. 这表明该演示代码是在WPF应用而不是控制台应用的上下文中编写的。

If you just change your code to: 如果仅将代码更改为:

Console.WriteLine(output.ToString());

then you should be fine. 那你应该没事的

However, I'd strongly recommend using LINQ to XML and XDocument instead. 但是,我强烈建议改用LINQ to XML和XDocument Reading an XML document is very simple: 读取XML文档非常简单:

String xmlString = @"<?xml version='1.0'?>
    <!-- This is a sample XML document -->
    <Items>
      <Item>test with a child element <more/> stuff</Item>
    </Items>";
XDocument doc = XDocument.Parse(xmlString);

You can then find specific elements in the document etc. Start on the LINQ to XML "root" page on MSDN for more details. 然后,您可以在文档等中找到特定的元素。有关更多详细信息,请从MSDN上的LINQ to XML“根”页面开始

In fact in this sample, you must add writer.Flush(); 实际上,在此示例中,您必须添加writer.Flush();。 at the end of while (reader.Read()) loop otherwise outpout stay empty. 在while(reader.Read())循环结束时,否则outpout保持为空。 after to display output value, change code to : Console.WriteLine(output.ToString()); 在显示输出值之后,将代码更改为:Console.WriteLine(output.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM