0

Though it seems to be a very trivial question I am trying to understand XMLReader Class and it's members. Given a XML file I want to opt out of printing XML Declaration

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

                }
            }
           Console.WriteLine( output.ToString());
           Console.ReadKey();
        }

But if I comment out

  case XmlNodeType.XmlDeclaration:
                                case XmlNodeType.ProcessingInstruction:
                                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                    break;

I still get to see the XML Declaration i.e.

<?xml version='1.0'?>

What's wrong here ? Again if I comment out

 case XmlNodeType.Element:
 writer.WriteStartElement(reader.Name);
 break;

it says InvalidOperationException was unhandled . Could you please explain ? I am not getting the whole picture.

2
  • 1
    As it is saying WriteStartElement, this is that START element which has to be there. This article might give you some better understanding. csharp.net-tutorials.com/xml/… Commented Sep 6, 2015 at 13:48
  • Thanks but how would I not print the initial XML Declaration . Commenting out those lines do not work. Commented Sep 6, 2015 at 13:50

2 Answers 2

2

Set ws.OmitXmlDeclaration = true; in your code to omit xml declaration.

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.OmitXmlDeclaration = true;
            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;
                    }
                }

            }
        }
       Console.WriteLine( output.ToString());
       Console.ReadKey();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The writing of the declaration is a feature of the XmlWriter. You can opt out of this using XmlWriterSettings.

var settings = new XmlWriterSettings
{
     OmitXmlDeclaration = true
};

using (XmlWriter writer = XmlWriter.Create(output, settings))

See the documentation for full details of all settings available.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.