Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6233359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:17:07+00:00 2026-05-24T10:17:07+00:00

I have a helper method that serialises an object, which works until you try

  • 0

I have a helper method that serialises an object, which works until you try to change the encoding… when received by the consumer web service, appears to be incorrect with some strange characters.

Here is the log entries from the app,

UTF-16 (this works):

2011-08-09 11:16:03,140 DEBUG SomeRestfulService *   xmlData    <?xml version="1.0" encoding="utf-8"?>
<loginRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserName>Admin</UserName>
  <Password>Password</Password>
  <MarketCode>GB</MarketCode>
</loginRequest>

UTF-8 (notice the strange character):

2011-08-09 11:21:30,687 DEBUG SomeRestfulService *   xmlData    <?xml version="1.0" encoding="utf-8"?><loginRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><UserName>Admin</UserName><Password>Password</Password><MarketCode>GB</MarketCode></loginRequest>

I don’t know why it is has lost the layout.

Helper method:

Public Shared Function SerializeObject(ByVal obj As Object, ByVal encoding As Text.Encoding) As String

    Dim serializer As New XmlSerializer(obj.GetType)

    If encoding Is Nothing Then
        Using strWriter As New IO.StringWriter()
            serializer.Serialize(strWriter, obj)
            Return strWriter.ToString
        End Using
    Else
        Using stream As New IO.MemoryStream, xtWriter As New Xml.XmlTextWriter(stream, encoding)
            serializer.Serialize(xtWriter, obj)
            Return encoding.GetString(stream.ToArray())
        End Using
    End If


End Function

Note: If I pass encoding as nothing, the default encoding is UTF-16, everything is ok, originally I never had the encoding part, but it is a requirement, so needs to be in there.

Am I doing the serialising incorrectly when encoding to UTF-8? How can I fix this?

I tried the following to omit the BOM, but still have the same problem:

Dim utf8 As New Text.UTF8Encoding(True)
Using stream As New IO.MemoryStream, xtWriter As New Xml.XmlTextWriter(stream, utf8)
    serializer.Serialize(xtWriter, obj)
    Return utf8.GetString(stream.ToArray())
End Using
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:17:08+00:00Added an answer on May 24, 2026 at 10:17 am

    What you’re seeing is the byte order mark (BOM) that is often used at the start of text files or streams to indicate the byte order and the Unicode variant.

    Your serializer is very strange. If you encode a string with some encoding such as UTF-8, you have to return it as an array of bytes. By first encoding the the XML in UTF-8 and then decoding the UTF-8 stream back to a string, you gain nothing (except introducing the problematic BOM).

    Either go with UTF-16 only or return a byte array. As the function is now, the encoding just introduces problems.

    Update:

    Based on the code in the comment below, I’ll see two approaches:

    Approach 1: Create a string with the serialized data and convert it to UTF-8 late

    Public Shared Function SerializeObject(ByVal obj As Object) As String
    
        Dim serializer As New XmlSerializer(obj.GetType)
    
        Using strWriter As New IO.StringWriter()
            serializer.Serialize(strWriter, obj)
            Return strWriter.ToString
        End Using
    
    End Function
    
    ....
    
    Dim serialisedObject As String = SerializeObject(object)
    Dim postData As Byte() = New Text.UTF8Encoding(True).GetBytes(serialisedObject)
    

    If you need a differnt encoding, change the last line. If you want to omit the byte order mark, pass False to UTF8Encoding().

    Approach 2: Create the properly encoded data in the first place and continue with a byte array

    Public Shared Function SerializeObject(ByVal obj As Object, ByVal encoding As Text.Encoding) As Byte()
    
        Dim serializer As New XmlSerializer(obj.GetType)
    
        If encoding Is Nothing Then
           Set encoding = Encoding.Unicode
        End If
    
        Using stream As New IO.MemoryStream, xtWriter As New Xml.XmlTextWriter(stream, encoding)
            serializer.Serialize(xtWriter, obj)
            Return stream.ToArray()
        End Using
    
    End Function
    
    
    ....
    
    Dim postData As Byte() = SerializeObject(object)
    

    In this case, the XmlTextWriter directly encodes the data with the correct encoding. As since we have a byte array already, the last step is shorter: we directly have the data to send to the client.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object obj that is passed into a helper method. public static
I have a helper assembly which includes a function to identify object types: namespace
I have a helper method that retrieves a string that I need to get
In my Rails app I have a helper method location that gets the coordinates
I have a helper method that creates navigation links for some controllers. def gen_associations(controllers)
So I have a helper method that looks something like the following: private D
I have a helper method that calls two other helper methods, the problem is
I have been thinking about making a helper method that shortens internal links. For
I have a helper method which encrypts some data on the iPhone. If the
I have a Scala helper method that currently tries to fetch a URL and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.