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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:47:40+00:00 2026-05-22T20:47:40+00:00

My problem is really strange.I need to write a huge xml file from different

  • 0

My problem is really strange.I need to write a huge xml file from different sources in c#.

So first to begin with,I use XmlTextWriter to write to a memory stream:

     MemoryStream ms=new MemoryStream();
     XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        **settings.Encoding = new UTF8Encoding(false,false); // set UTF8 without BOM !!**
        XmlWriter xmlWriter = XmlWriter.Create(ms, settings);
        xmlWriter.WriteStartDocument();

        xmlWriter.WriteStartElement();

        xmlWriter.WriteEndElement(); 
        xmlWriter.WriteEndDocument()

        xmlWriter.Flush();

        xmlWriter.Close();

Note that ms will not be closed after closing xmlTextWriter. So I pass ms to a xmlDocument and write other parts of xml elements:

        XmlDocument doc = new XmlDocument();
        ms.Position=0;
        doc.Load(ms);

Then I write by using:

       XmlElement elmRoot=doc.DocumentElement(); //root
       XmlElement elm=doc.CreateElement("New Element");
       XmlAttribute att=doc.CreateAttribute("att");
       att.Value="ABC";
       elm.Attributes.Append(att);
       elmRoot.AppendChild(elm);
       ms.Position=0;
       doc.Save(ms)

However, after the above operations, I found that ms still has three BOM at the beginning of generated xml file, causing the file cannot be opened in a browser. Any ideas?

Thanks a lot!

  • 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-22T20:47:40+00:00Added an answer on May 22, 2026 at 8:47 pm

    I don’t know why you’re overwriting the MemoryStream again, that seems a bad idea.

    But if you need the XDocument then you can use the XDocument.Save(XmlWriter writer) option and make sure you create the writer with the no-BOM settings as in the beginning of your code.

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

Sidebar

Related Questions

I've a really strange problem getting IIS7 to connect to Sql2008. First, using the
i have a strange apache mod_rewrite problem. I need to hide a sub-directory from
I am currently stuck at a really strange problem with GDI and timers. First
This is a really strange problem, that appears to be somewhat intermittent (although it
I use boost::shared_ptr in my application in C++. The memory problem is really serious,
I've come across a really strange problem. I have written a simple Deck class
This is really strange to me and it's becoming into a real problem. I'm
I have a really strange problem with the standard logging module used in django
This is a really strange problem I'm seeing in my app. I have an
i'm having some really strange problem. i wrote a filemanager in PHP with the

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.