Search code examples
c#xmldoctype

How to add a document type to a new XML file in C#


How do I add the following document type to the begining of an XML file

<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">

I am trying to create an XML file using C# XDocument but cant figure out how to add the above to the beginning of the xml file


Solution

  • You need to add an XDocumentType to the beginning of the XDocument:

    var xDocument = new XDocument(
      new XDocumentType(
        "smil",
        "-//W3C//DTD SMIL 2.0//EN",
        "http://www.w3.org/2001/SMIL20/SMIL20.dtd",
        null
      ),
      new XElement("Root")
    );