locked
Data utf-8 encoded, need to post XML as ISO-8859-1 encoded RRS feed

  • Question

  • User-1896913385 posted

    Hi,

    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.

    Thursday, May 22, 2014 5:35 PM

Answers

  • User-1896913385 posted

    Today, the owner of the webservice changed the service to use utf-8 encoding. So there's no problem anymore. Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 5:02 PM

All replies

  • User281315223 posted

    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)
    End Function
    
    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)
    		form.Add(name, value)
    	Next
    	Return form
    End Function

    which would be used via :

    GetEncodedForm(Request.InputStream,Encoding.GetEncoding("ISO-8859-1"))

    If you needed to encode a single string (such as a message), you could also use something like :

    Encoding.UTF8.GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(yourString))

    Thursday, May 22, 2014 5:48 PM
  • User-1896913385 posted

    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.

    Thursday, May 22, 2014 6:06 PM
  • User1918509225 posted

    Hi Mdv,

    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.

    Best Regards,

    Kevin Shen.

    Friday, May 23, 2014 1:49 AM
  • User-1896913385 posted

    Today, the owner of the webservice changed the service to use utf-8 encoding. So there's no problem anymore. Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 23, 2014 5:02 PM