简体   繁体   中英

Parsing an XML file using C#

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 ie

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

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

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.

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