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

The Archive Base Latest Questions

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

I have created a method that accepts an object then attempts to serialize the

  • 0

I have created a method that accepts an object then attempts to serialize the object to Xml by first using the XmlSerializer to serialize to a string, then load the Xml back into an XmlDocument object for the method to return. The code looks like this;

 public static XmlDocument ConvertObjectToXMLMessage(object ObjectToConvert)
    {
        MemoryStream stream = null;
        XmlWriter writer = null;
        XmlSerializer serializer = null;
        XmlDocument xmlDoc = new XmlDocument();
        UnicodeEncoding utf = new UnicodeEncoding();
        UTF8Encoding utf8 = new UTF8Encoding();
        ASCIIEncoding ascii = new ASCIIEncoding();
        string result = string.Empty;

        try
        {
            stream = new MemoryStream();
            //writer = new StreamWriter(stream, Encoding.Unicode);
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            writer = XmlWriter.Create(stream, settings);                

            serializer = new XmlSerializer(ObjectToConvert.GetType());
            serializer.Serialize(writer, ObjectToConvert);

            int count = Convert.ToInt32(stream.Length);
            Byte[] arr = new Byte[count];

            stream.Seek(0, SeekOrigin.Begin);

            stream.Read(arr, 0, count);

            result = utf8.GetString(arr).Trim();

            // if this is being used during a debug session, the xml will be written to the Debug Console
#if DEBUG
            //blank line before
            Debug.WriteLine(string.Empty);
            // output result
            Debug.Write(result);
            //blank line after
            Debug.WriteLine(string.Empty);
#endif

            xmlDoc.LoadXml(result);

            return xmlDoc;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (writer != null)
                writer.Close();
        }
    }

All works well until the xmlDoc.LoadXml(result) command. This throws an exception;
{“Data at the root level is invalid. Line 1, position 1.”}

As you can see, I have declared a number of encoding variables. If I use the ASCII encoding, it works. I need to use UTF8.

Any ideas why this does not work? I think it is because of spurious characters being inserted at the start of the Xml ofter serialisation. How do I avoid this? The types of classes that I could be serializing would be entity framework objects or proxy classed generated from XSD’s or WSDL.

  • 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-17T15:36:07+00:00Added an answer on May 17, 2026 at 3:36 pm

    What if you load the stream directly without manually reading it or converting it?

    XmlDocument xd = new XmlDocument();
    xd.Load( stream );
    

    This will at least reduce the number of places an error could be occurring.

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

Sidebar

Related Questions

I have created a console application that calls a method on a webservice. I
I have a method that creates a MessageDigest (a hash) from a file, and
I have a method that's about ten lines of code. I want to create
I would like to create a safe sum extension method that would have the
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp
Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
I have created a .NET DLL which makes some methods COM visible. One method
I have a method running in a seperate thread. The thread is created and
I have created a web service which has a couple of methods developed using
Suppose I have BaseClass with public methods A and B, and I create DerivedClass

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.