Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlDocument.Save() inserts empty square brackets in doctype declaration

Tags:

c#

xml

Everytime I call the method on

XmlDocument.Save(fooFilepath);

it inserts two square brackets at the end of the DOCTYPE tag e.g.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ARCXML SYSTEM "G:\ArcIMS\DTD\arcxml.dtd"[]>

Does anyone know why this might happen? I obviously don't want this to happen.

like image 983
Vidar Avatar asked May 31 '11 18:05

Vidar


2 Answers

That is a normal (and optional) part of a DOCTYPE declaration.

<!DOCTYPE rootname SYSTEM url [DTD]>

Where DTD contains any internal subset declarations to your document.

like image 59
user7116 Avatar answered Nov 04 '22 21:11

user7116


The underlying reader used by XmlDocument (which uses XmlTextReader) does not distinguish between a document with an empty internal subset and one with no internal subset specified, so it will return InternalSubset == "" for both cases.

Then when XmlDocument.Save() is called, it sees an empty string for InternalSubset and dutifully writes an empty internal subset: [].

Unfortunately, XmlDocument.DocumentType.InternalSubset is readonly, so you cannot set it to null. You can either do:

  1. Use the lower level XmlTextWriter.WriteDocType() to have more control.

  2. Use XDocument, where you can set XDocument.DocumentType.InternalSubset = null.

like image 38
wisbucky Avatar answered Nov 04 '22 19:11

wisbucky