简体   繁体   中英

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

I am using the following Microsoft sample code to read 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.

I tried to put a var before this but still get an error. 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?

But the answer was just to use 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 . That suggests the demo code was written in the context of a WPF app rather than a console app.

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. Reading an XML document is very simple:

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.

In fact in this sample, you must add writer.Flush(); at the end of while (reader.Read()) loop otherwise outpout stay empty. after to display output value, change code to : Console.WriteLine(output.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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