Groups | Blog | Home
all groups > dotnet xml > january 2005 >

dotnet xml : Suppress encoding attribute - XMLTextWriter


Josh Newman
1/19/2005 12:00:32 PM
I'm using the XMLTextWriter to create an XML document. I do not want the
encoding attribure in the XML file.

Instead of:
<?xml version="1.0" encoding="utf-8"?>

I want:
<?xml version="1.0"?>

Code extract:
MemoryStream ms= new MemoryStream();
XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);

Martin Honnen
1/19/2005 3:46:14 PM


[quoted text, click to view]

Why? If you save as UTF-8 there is nothing wrong with encoding="utf-8"
in the XML declaration.

[quoted text, click to view]

The XML declaration is written by the WriteStartDocument method but this
doesn't seem to have an overload to suppress the encoding.
I guess it should be possible to write your own class extending
XmlTextWriter where you then override the method WriteStartDocument to
make sure it only outputs the XML declaration with the version but with
no encoding. I have not tested that approach though.

As a sort of hack you could also try to use XmlTextWriter and then not
to call WriteStartDocument but rather WriteProcessingInstruction with
the proper arguments so that a processing instruction looking like an
XML declaration is written. But that is certainly a hack and I haven't
tested that either.


--

Martin Honnen
Josh Newman
1/19/2005 6:29:51 PM
I need to remove the "encoding" part because the XML parser on one of our
legacy systems blows-up if you include and encoding.
It is a third-party component that we cannot change so I have no choice but
to remove it.

[quoted text, click to view]

AddThis Social Bookmark Button