all groups > dotnet xml > july 2007 >
You're in the

dotnet xml

group:

response.writing out UTF8 XML. How?


response.writing out UTF8 XML. How? darrel
7/17/2007 4:30:38 PM
dotnet xml:
I was having issues before with my XML being sent to the browser as UTF16.
Via my XmlTextWriter, was able to fix this by explicitely setting the
encoding to UTF8:

Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

That fixed the issue.

I'm not generating an XML file using a different method grabbing XML from a
DB and then reading it in as a stringreader, then transforming that via an
XSLT file.

Eventually, I do the XSLT transformation and response.write the result to
the browser.

The problem is that, once again, it's being set witn a UTF16 encoding.

I've tried to explicitely set the response encoding as such:

-------------
xslt.Transform(doc, xslArg, sw, Nothing)
Response.ContentType = "text/xml"
Response.ContentEncoding = System.Text.Encoding.UTF8
Response.Write(sw.ToString)
Response.End()
-------------

But I STILL get the UTF16 declaration in my XML:

<?xml version="1.0" encoding="utf-16"?><rss version="2.0">...

Why!?

-Darrel

Re: response.writing out UTF8 XML. How? Martin Honnen
7/18/2007 12:00:00 AM
[quoted text, click to view]

Don't transform to a StringWriter, instead transform directly to
Response.Output:
Response.ContentType = "application/xml"
xslt.Transform(doc, xslArg, Response.Output, Nothing)
And make sure your XSLT stylesheet has
<xsl:output method="xml" encoding="UTF-8"/>


--

Martin Honnen --- MVP XML
Re: response.writing out UTF8 XML. How? darrel
7/18/2007 10:56:10 AM
[quoted text, click to view]

Aha!

So, I have this now:

Response.ContentType = "text/xml"
Response.ContentEncoding = System.Text.Encoding.UTF8
xslt.Transform(doc, xslArg, Response.Output, Nothing)
Response.End()

And that works! However, I've never epxlicitely put the <?xml...> tag in my
XSLT. It seems that this is generated for me via the response output. Should
I be explicitly including this in my XSLT file?

-Darrel

Re: response.writing out UTF8 XML. How? Martin Honnen
7/19/2007 5:46:31 PM
[quoted text, click to view]

The XSLT document and its encoding are not related to the encoding of
the XSLT result. An XSLT document is just an XML document and for an XML
document the XML declaration (i.e. <?xml version="..." encoding="..."?>)
is optional if the version is 1.0 and the encoding is UTF-8 or UTF-16.


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button