15

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.

dtb
  • 213,145
  • 36
  • 401
  • 431
Vidar
  • 6,548
  • 22
  • 66
  • 96
  • 3
    The square brackets are the place where internal subset declarations are placed. If you don't have such declarations, the brackets are empty and may be omitted. But it's not wrong to have empty brackets. Any standards-conforming XML parser should be able to deal with the generated doctype declaration, so the only issue you have is an aesthetic one. – dtb May 31 '11 at 18:44
  • After checking out the `XmlWriter.WriteDocType()` implementations, I'm puzzled why a new `XDocumentType` with `internalSubset` set to `null` outputs the empty squary brackets.In my case, I'm writing to Response.Output. Before calling `Save()`, I confirmed that `doc.DocumentType.InternalSubset` is null. All implementations seem to skip outputting `[]` but at runtime it's written to the XML by something. – Alex Kamburov Mar 25 '14 at 12:27

2 Answers2

6

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
5

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.

wisbucky
  • 33,218
  • 10
  • 150
  • 101