For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > April 2007 > I need UTF-8. But I keep getting UTF-16. Why? How to fix?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author I need UTF-8. But I keep getting UTF-16. Why? How to fix?
darrel

2007-04-30, 7:08 pm

I've been struggling for some time now getting a RSS app to work. I'm
creating RSS from existing XML files (transforming via XSLT).

The problem is that the page, itself, is still being sent at UTF-16 when I
create the RSS from XSLT. It's UTF-8 when I'm creating the XML myself via
the DB and an XMLTextWriter.

This gives IE headaches. It adds a space between each character in the XML
and doesn't render it as an XML file.

I'm completely lost as to what, exactly, is causing the page to be sent as
UTF-16 and how/where to fix it. The key code is below:


========================================
==

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Clear()
Response.ContentType = "text/xml"
Response.ContentEncoding = Encoding.Unicode

...
buildNodeRSS(Request.QueryString("pageID"))
...

End Sub


Public Sub buildNodeRSS(ByVal pageId As Integer)

Dim siteID As Integer
' query the DB to find out which site this particular node belongs to
....code to grab the siteID...
End Try
' set up the XML file
Dim XMLfile As String
theMenu = retrieveMenuXML.getMenuXML(siteID)
XMLfile = theMenu.strMenu




'check to see if the file exists in the cache
If System.Web.HttpContext.Current.Cache(siteID & "menuXML") Is Nothing Then
'read in the XML file
Dim theMenuXML As menuXML = retrieveMenuXML.getMenuXML(siteID)
XMLfile = theMenuXML.strMenu
'add the text to the cache object and set timeout
System.Web.HttpContext.Current.Cache.Insert(siteID & "menuXML", XMLfile,
Nothing, DateTime.Now.AddMinutes(0.5), TimeSpan.Zero)
Else
'just use the cached version
XMLfile = CType(System.Web.HttpContext.Current.Cache(siteID & "menuXML"),
String)
End If

styleSheetFile = "lastUpdatesRSS.xslt"
Dim stylesheet As String = (MapPath(styleSheetFile))
'Create the XslTransform and load the stylesheet.
Dim xslt As XslTransform = New XslTransform
xslt.Load(stylesheet)

'Load the XML data file.
Dim doc As XPathDocument = New XPathDocument(sr)

'Create an XsltArgumentList.
Dim xslArg As XsltArgumentList = New XsltArgumentList

xslArg.AddParam("pageID", "", pageId)
'create the default link prefix param
Dim linkPrefix As String

linkPrefix = "/"

xslArg.AddParam("linkPrefix", "", linkPrefix)

Dim ms As MemoryStream = New MemoryStream
xslt.Transform(doc, xslArg, ms, Nothing)
ms.Position = 0
Dim StReader As StreamReader = New StreamReader(ms, Encoding.UTF8)
'Response.Write(sw.ToString)
Response.Write(StReader.ReadToEnd().ToString)
End Sub

--
========================================
=========================
Win prizes searching google:
http://www.blingo.com/friends?ref=h...FnTqhv-2GE1FNtA


darrel

2007-04-30, 7:08 pm

> Response.ContentEncoding = Encoding.Unicode

I'm a moron. I've been staring at the above for a while and didn't notice
that 'duh...I'm SETTING it to UTF-16 via unicode'.

Encoding.utf-8 fixed it.

-Darrel


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com