0

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.

Community
  • 1
  • 1
Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Did you consider using `XDocument` instead? By my opinion much easier to use than `XmlReader`. – user1908061 Jun 16 '13 at 06:57
  • Thanks - I don't even know about XDocument. I never had to do this before. I will have a look at that. I am still hoping someone can give me an example to get me started. I'm totally out of my depth with XML. – Alan2 Jun 16 '13 at 07:02

2 Answers2

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon. Thanks for your suggestions. I will look into what you suggested. Are LINQ to XML and XDocument used together or is it one or the other? – Alan2 Jun 16 '13 at 07:03
  • @Gemma: `XDocument` is a type within the LINQ to XML API. – Jon Skeet Jun 16 '13 at 07:04
  • Thanks Jon. I will mark this as correct and go and have a look at using what you suggest. – Alan2 Jun 16 '13 at 07:06
0

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