.NET XmlDocument : Why DOCTYPE changes after Save?

c# xmldocument selectsinglenode
xmldocument selectnodes
c# xmlnode
xmldocument save
c# xml example
create xml document c#
c# xmldocument example
system.xml.xmldocument powershell

I am opening a XML file using .NET XmlReader and saving the file in another filename and it seems that the DOCTYPE declaration changes between the two files. While the newly saved file is still valid XML, I was wondering why it insisted on changing original tags.

Dim oXmlSettings As Xml.XmlReaderSettings = New Xml.XmlReaderSettings()
oXmlSettings.XmlResolver = Nothing
oXmlSettings.CheckCharacters = False
oXmlSettings.ProhibitDtd = False
oXmlSettings.IgnoreWhitespace = True

Dim oXmlDoc As XmlReader = XmlReader.Create(pathToOriginalXml, oXmlSettings)
Dim oDoc As XmlDocument = New XmlDocument()

The following (in the original document):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">

becomes (notice the [ ] characters at the end):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"[]>

Probably the library parses the DOCTYPE element into an internal structure and then converts the structure back to text. It doesn't store the original string form.

XmlDocument.Save() adding quirky doctype - MSDN, NET Framework Class Libraries Save() is changing <!DOCTYPE html> to <! DOCTYPE html []> and this is seen as quirky by the HTML5 validators. / 41710149/c-sharp-xmldocument-save-adding-quirky-doctype, and I am  Remarks. The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2.

There is a bug in System.Xml when you set XmlDocument.XmlResolver = null. The workaround is to create a custom XmlTextWriter:

    private class NullSubsetXmlTextWriter : XmlTextWriter
        public NullSubsetXmlTextWriter(String inputFileName, Encoding encoding)
            : base(inputFileName, encoding)
        public override void WriteDocType(string name, string pubid, string sysid, string subset)
            if (subset == String.Empty)
                subset = null;
            base.WriteDocType(name, pubid, sysid, subset);

In your code, create a new NullSubsetXmlTextWriter(pathToNewXml, Encoding.UTF8) and pass that object to the oDoc.Save() method.

Here is the Microsoft support case where you can read about the workaround (it describes the workaround but doesn't provide the code).

XmlDocument Class (System.Xml), Represents an XML document. After you get a node, you get the value of attributes or child nodes. the author's last name is Austen, and then changes the price of those books. Gets the node containing the DOCTYPE declaration. Gets or sets a value indicating whether to preserve white space in element content. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Jobs Programming and related technical career opportunities

This is best solution for you:

writer.WriteDocType("Name", Nothing, 
                    "http://xml.cxml.org/schemas/cXML/1.2.033/Fulfill.dtd", Nothing) 

If you use Nothing you will not get [] or "" etc

.NET & XML: Understanding the Code and Markup Behind the Wizards, XmlDocument document = new XmlDocument( ); document. In order to make this change, you would need to remove the DOCTYPE node from all ofthe XML Save(file + ".new"); } This process can be repeated with any type of XmlNode. The keyword DOCTYPE must be followed by the name of the root element in the XML document. The keyword DOCTYPE must be in upper case. The External DTD: External DTDs are useful for creating a common DTD that can be shared between multiple documents. Any changes that are made to the external DTD automatically updates all the documents that

Preserving Doctype Information, The difficulty comes in working out how to handle inconsistent doctypes, as it is not feasible for an output XML document to have more than one doctype. Here  Save(Stream) Saves the XML document to the specified stream. (Inherited from XmlDocument) Save(String) Saves the XML document to the specified file. If the specified file exists, this method overwrites it. (Inherited from XmlDocument) Save(TextWriter) Saves the XML document to the specified TextWriter. (Inherited from XmlDocument) Save(XmlWriter)

Pro Visual C++/CLI and the .NET 3.5 Platform, PreviousSibling is the XmlNode with the same parent immediately before the current node. It has a InsertAfter() inserts a node immediately after the current node. WriteContentTo() saves all the children of the XmlDocument to an XmlWriter. Any changes that you make to the XmlNodes in the DOM tree are immediately  Examples. The following example returns the first book with the matching author name. The XmlNamespaceManager resolves the default namespace in the XPath expression.. #using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; int main() { XmlDocument^ doc = gcnew XmlDocument; doc->Load( L"newbooks.xml" ); // Create an XmlNamespaceManager to resolve

Fix Your Site With the Right DOCTYPE! – A List Apart, XHTML is an application of XML. A well-formed XML document is simply one that nests elements properly (i.e. not overlapping like ), includes a trailing slash in  doc.Save(Console.Out); About Sample Example 5 Here is one example of how to load an XML document using XmlTextReader. In this sample example, we read books.xml file using XmlTextReader and call its Read method. After that we call XmlDocumetn's Load method to load XmlTextReader contents to XmlDocument and call Save method to save the document.