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 6212049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:26:37+00:00 2026-05-24T06:26:37+00:00

I need to get the serialized XML representation of an object as a string.

  • 0

I need to get the serialized XML representation of an object as a string. I’m using the XmlSerializer and a memoryStream to do this.

XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
using (MemoryStream stream = new MemoryStream())
{
  using (XmlTextWriter writer = new XmlTextWriter(stream,Encoding.UTF8))
  {
    serializer.Serialize(writer, myClass);
    string xml = Encoding.UTF8.GetString(stream.ToArray());
    //other chars may be added from the encoding.
    xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
    xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
    return xml;
  }
}

Now just take note of the xml.substring lines for a moment. What I’m finding is that (even thought I’m specifying encoding on the XmlTextWriter and on the GetString (and I’m using memoryStream.ToArray(), so I’m operating only on the data in the stream’s buffer)… the resulting xml string has some non-xml happy character added. In my case, a ‘?’ at the start of the string. This is why I’m substring-ing for ‘<‘ and ‘>’ to ensure I’ve only getting the good stuff.

Strange thing is, looking at this string in the debugger (Text Visualizer), I don’t see this ‘?’. Only when I paste what’s in the visualizer into notepad or similar.

So while the above code (substring etc) does the job, what’s actually happening here? Is some unsigned byte thing being included and not being represented in the Text Visualizer?

  • 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-24T06:26:38+00:00Added an answer on May 24, 2026 at 6:26 am

    You can exclude the BOM by specifying the encoding specifically – i.e. instead of Encoding.UTF8, try using:

    using (MemoryStream stream = new MemoryStream())
    {
      var enc = new UTF8Encoding(false);
      using (XmlTextWriter writer = new XmlTextWriter(stream,enc))
      {
        serializer.Serialize(writer, myClass);        
      }
      string xml = Encoding.UTF8.GetString(
          stream.GetBuffer(), 0, (int)stream.Length);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to store an xml serialized object in a cookie, but i get
I'm currently serializing an object using XMLSerializer, and the resulting XML starts with: <?xml
I am using an XmlSerializer to serialize an object to xml. After the object
I need to serialize an object like this: public class Book { public string
I need to get the default printer name. I'll be using C# but I
I've got an object that is being serialized / deserialized via the XmlSerializer in
I'm using a simple ajax-enabled WCF service. I'm creating a string of XML on
This is my current code: public void WriteXml(System.Xml.XmlWriter writer) { CloseConnection(); using (Stream source
I am using following code to get List from xml file - public static
I have an Document object that is modeling a XML like this one: <RootNode

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.