Connecting Tech Pros Worldwide Forums | Help | Site Map

Suppress encoding attribute - XMLTextWriter

Josh Newman
Guest
 
Posts: n/a
#1: Nov 12 '05
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
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Suppress encoding attribute - XMLTextWriter




Josh Newman wrote:
[color=blue]
> 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"?>[/color]

Why? If you save as UTF-8 there is nothing wrong with encoding="utf-8"
in the XML declaration.
[color=blue]
> Code extract:
> MemoryStream ms= new MemoryStream();
> XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);[/color]

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
http://JavaScript.FAQTs.com/
Josh Newman
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Suppress encoding attribute - XMLTextWriter


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.

"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:OIAdjWj$EHA.1904@TK2MSFTNGP14.phx.gbl...[color=blue]
>
>
> Josh Newman wrote:
>[color=green]
>> 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"?>[/color]
>
> Why? If you save as UTF-8 there is nothing wrong with encoding="utf-8" in
> the XML declaration.
>[color=green]
>> Code extract:
>> MemoryStream ms= new MemoryStream();
>> XmlTextWriter x = new XmlTextWriter(ms, Encoding.UTF8);[/color]
>
> 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
> http://JavaScript.FAQTs.com/[/color]


Closed Thread