XmlDocument.Save() adding quirky doctype RRS feed

  • Question

  • I am doing some processing of HTML (as XML). All works well, except for one small irritation: XmlDocument.Save() is changing <!DOCTYPE html> to <!DOCTYPE html []> and this is seen as quirky by the HTML5 validators. I could not find a way to prevent XmlDocument.Save() doing this. Could anyone help, please.

    I have asked for help on StackOverflow:, and I am cross-posting it here to reach the MSDN community.

    The following code

    using System.Xml;
    class Program
      static void Main(string[] args)
        if (args.Length != 1) 
        string infile = args[0];
        XmlDocument doc = new XmlDocument();
        XmlNode root = doc.DocumentElement;
        XmlWriterSettings xws = new XmlWriterSettings
          OmitXmlDeclaration = true,
          Indent = true,
            IndentChars = "   ",
        // Some custom processing - TODO
        using (XmlWriter xw = XmlWriter.Create("output.html", xws))
      } // Main
    } // class

    consuming the input:

    <!DOCTYPE html>
       <meta charset="utf-8" />

    produces an output that is exactly the same as the input except that the doctype sees those square brackets at the end.

    Would someone be able to help me get rid of those pesky square brackets.

    Thanks in advance!

    Wednesday, January 18, 2017 10:28 AM


  • Try this workaround:

        var dt = doc.DocumentType;
        doc.RemoveChild( dt );
        . . .
        xw.WriteDocType( "html", null, null, null );

    • Marked as answer by B1ue Pengu1n Wednesday, January 18, 2017 7:21 PM
    Wednesday, January 18, 2017 12:52 PM