c#
  • xml
  • 2015-09-06 70 views 0 likes 
    0

    雖然它似乎是一個非常微不足道的問題,但我試圖理解XMLReader類,它是成員。鑑於我想退出打印XML聲明使用C解析XML文件#

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

    的XML文件,但如果我註釋掉

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

    我仍然能看到XML聲明即

    <?xml version='1.0'?> 
    

    什麼問題這裏 ? 再次,如果我註釋掉

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

    它說InvalidOperationException異常是未處理的。 你能解釋一下嗎?我沒有得到全部的圖片。

    +1

    正如WriteStartElement所說的,這是START元素必須在那裏。本文可能會給你一些更好的理解。 http://csharp.net-tutorials.com/xml/writing-xml-with-the-xmlwriter-class/ –

    +0

    謝謝,但我怎麼會不打印最初的XML聲明。評論這些行不起作用。 – StrugglingCoder

    回答

    1

    該聲明的寫作是XmlWriter的一項功能。您可以使用XmlWriterSettings退出此選項。

    var settings = new XmlWriterSettings 
    { 
        OmitXmlDeclaration = true 
    }; 
    
    using (XmlWriter writer = XmlWriter.Create(output, settings)) 
    

    爲所有可用設置的完整詳情,請參閱the documentation

    2

    在您的代碼中設置ws.OmitXmlDeclaration = true;以省略xml聲明。

    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(); 
        } 
    
    相關問題