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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:13:50+00:00 2026-06-11T15:13:50+00:00

Person person = GetPerson(); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty); XmlSerializer serializer =

  • 0
            Person person = GetPerson();
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, string.Empty);
            XmlSerializer serializer = new XmlSerializer(typeof(Person));
            string personText = string.Empty;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings() { Encoding = Encoding.UTF8 }))
                {
                    serializer.Serialize(xmlWriter, person, ns);
                    xmlWriter.Flush();
                    personText = Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }

            string path = @"D:\person.xml";
// Write method 1:
            File.WriteAllText(path, personText);

// Write method 2:
            using (StreamWriter streamWriter = new StreamWriter(path, false , Encoding.UTF8))
            {
                streamWriter.Write(personText);
            }

// Read the xml
            using (FileStream fileStream = new FileStream(path, FileMode.Open))
            {
                return XDocument.Load(XmlReader.Create(fileStream));
            }

When I read the xml after writing using method 2, I get this Data at the root level is invalid. Line 1, position 1. But it works fine using method 1.

What is causing this? Any pointers appreciated.

  • 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-06-11T15:13:52+00:00Added an answer on June 11, 2026 at 3:13 pm

    The problem is that both the StreamWriter and the XmlWriter are adding a byte-order-mark.

    Options:

    • String the BOM from personText to start with
    • Pass new UTF8Encoding(false) instead of Encoding.UTF8 for the StreamWriter
    • Pass new UTF8Encoding(false) instead of Encoding.UTF8 for the XmlWriter
    • Avoid converting to text and back again in the first place: you’ve got the binary data in the MemoryStream, why not just dump that to disk?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create new application using BLToolkit but i can't compile the code,
class Person { string name; public Person(string name) { this.name = name; } public
Say I have a person table in a database. Whenever a new person is
I have class Person class person{ int ID ; sting FirstName ; string Last
I have the following class Person: public class Person { public string Name {
I'm using JavaScript to access a ScriptService method called GetPerson(). The problem is that
When trying to create a simple service to return a simple JSON string by
Lets say I have an object class Person { public string Name { get;
I'm trying to create a web service using Grails and Apache CXF, naturally using
Using reflection, is it possible to create an instance of a type that inherits

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.