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

D

darrel

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
 
D

darrel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top