writeXMLDecl

Writes the <?xml...?> declaration to the given output range. If it's going to be used in conjunction with XMLWriter, then either writeXMLDecl will need to be called before constructing the XMLWriter, or XMLWriter._output will need to be used to write to the output range before writing anything else using the XMLWriter. XMLWriter expects to be writing XML after the <?xml...?> and <!DOCTYPE...> declarations (assuming they're present at all), and it is invalid to put a <?xml...?> declaration anywhere but at the very beginning of an XML document.

void
writeXMLDecl
(
S
OR
)
(
ref OR output
)
if (
isOutputRange!(OR, char) &&
isSomeString!S
)

Parameters

S

The string type used to infer the encoding type. Ideally, it would be inferred from the type of the _output range, but unfortunately, the _output range API does not provide that functionality. If S does not match the encoding of the _output range, then the result will be invalid XML.

output
Type: OR

The _output range to write to.

Examples

1 import std.array : appender;
2 
3 {
4     auto app = appender!string();
5     app.writeXMLDecl!string();
6     assert(app.data == `<?xml version="1.0" encoding="UTF-8"?>`);
7 }
8 {
9     auto app = appender!wstring();
10     app.writeXMLDecl!wstring();
11     assert(app.data == `<?xml version="1.0" encoding="UTF-16"?>`w);
12 }
13 {
14     auto app = appender!dstring();
15     app.writeXMLDecl!dstring();
16     assert(app.data == `<?xml version="1.0" encoding="UTF-32"?>`d);
17 }
18 
19 // This would be invalid XML, because the output range contains UTF-8, but
20 // writeXMLDecl is told to write that the encoding is UTF-32.
21 {
22     auto app = appender!string();
23     app.writeXMLDecl!dstring();
24     assert(app.data == `<?xml version="1.0" encoding="UTF-32"?>`);
25 }

Meta