Last post May 23, 2014 05:02 PM by MdV
May 22, 2014 05:35 PM|MdV|LINK
The data in my database is utf-8 encoded, and my entire webapplication is based on utf-8 encoding.
But now I need to post some data as XML to a webservice that only works with ISO-8859-1 encoding.
How do I make sure that special characters like 'ë' don't get lost in the process?
Thanks in advance for any pointers!
Best regards, Marja
BTW. I'm using ASP.NET 4.5 and the VB.Net language.
May 22, 2014 05:48 PM|Rion Williams|LINK
I would think that UTF-8 (unicode) would work properly as ISO-8859-1 is
simply a subset of unicode characters. What happens when you try sending across UTF-8 encoded values?
You might try taking a look at
this related Stack Overflow discussion which details converting an existing form or data as ISO-8859-1 within your code behind. I've taken the liberty of convering the original code the top answer provided to Visual Basic for you :
Private Shared Function GetEncodedForm(stream As System.IO.Stream, encoding As Encoding) As NameValueCollection
' Read through your existing form in UT8'
Dim reader As New System.IO.StreamReader(stream, Encoding.UTF8)
' Return the encoded form in your preferred encoding'
Return GetEncodedForm(reader.ReadToEnd(), encoding)
Private Shared Function GetEncodedForm(urlEncoded As String, encoding As Encoding) As NameValueCollection
Dim form As New NameValueCollection()
Dim pairs As String() = urlEncoded.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
For Each pair As String In pairs
Dim pairItems As String() = pair.Split("=".ToCharArray(), 2, StringSplitOptions.RemoveEmptyEntries)
Dim name As String = HttpUtility.UrlDecode(pairItems(0), encoding)
Dim value As String = If((pairItems.Length > 1), HttpUtility.UrlDecode(pairItems(1), encoding), Nothing)
which would be used via :
If you needed to encode a single string (such as a message), you could also use something like :
May 22, 2014 06:06 PM|MdV|LINK
As part of a test XML message, I posted an utf-8 string like "Böning" and the webservice responded with "B├Âning" in its answer.
May 23, 2014 01:49 AM|Kevin Shen - MSFT|LINK
For your post ,i suggest that you can try the code below:
Dim iso As Encoding = Encoding.GetEncoding("ISO-8859-1")
Dim utf8 As Encoding = Encoding.UTF8
Dim utfBytes As Byte() = utf8.GetBytes(Message)
Dim isoBytes As Byte() = Encoding.Convert(utf8, iso, utfBytes)
Dim msg As String = iso.GetString(isoBytes)
Hope it can help you.
May 23, 2014 05:02 PM|MdV|LINK
Today, the owner of the webservice changed the service to use utf-8 encoding. So there's no problem anymore.