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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:14:04+00:00 2026-05-13T20:14:04+00:00

I am really a newbie with streams, so I don’t really know what I

  • 0

I am really a newbie with streams, so I don’t really know what I am doing here. 🙂

I have a XElement containing XML. I want it to return it as a file to the user.

  XElement xml = IndicesXMLGenerator.XML();

  //Code for creating a memorystream for returning to browser as file
   MemoryStream stream = new MemoryStream();
  XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.Unicode);
  xml.Save(writer);
  writer.Close();

  //Code for direct saving to harddisk
  FileStream filestream = new FileStream(@"D:\indices.xml", FileMode.Create);
  XmlTextWriter writer2 = new XmlTextWriter(filestream, System.Text.Encoding.Unicode);
  xml.Save(writer2);
  writer2.Close();
  filestream.Close();

  //Return memorystream as fileresult
  return base.File(new MemoryStream(stream.GetBuffer()), "text/xml", "AlleIndices.xml");
    }

When I open the file that I got from my browser, it is totally mangled.
like: �< ? X M L

When I change the encoding in the code to UTF8 it gives me a normal looking document, but at the end I get a lot of 0x0 characters that make the document invalid.

Strange thing is that the XML file that I saved directly to the harddisk from within the code is:

  • Perfectly fine in it’s encoding
  • Doesn’t contain any strange 0x0characters

So, what’s going on here? Why can’t I easily stream my XElement to the browser as file?

  • 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-13T20:14:05+00:00Added an answer on May 13, 2026 at 8:14 pm

    Calling GetBuffer will return the stream’s internal buffer, which will be larger than the actual data. (In case you write some more to the stream)

    You need to replace the call to GetBuffer with ToArray(), which will copy the used portion of the buffer to a new array.


    However, the best way to do this is return the original MemoryStream, but first set the Position property to 0, like this:

    stream.Position = 0;
    return File(stream, "text/xml", "AlleIndices.xml");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK - Python newbie here - I assume I am doing something really stupid,
I'm a newbie to threading, and I don't really know how to code a
Really newbie question: I have a .csv file that I need to read. I've
SVN newbie (although so far I really like it (coming from CVS) Ok, here's
Is is that I'm a newbie learning Ruby, or does it really have more
A newbie question (I haven't really created new projects in Eclipse yet): I have
I really hope would know how to do this cause I'm a total newbie
A really newbie question: under glassfish->domains->domain1->config folder, there's a file named keyfile, what is
I am really a newbie to Moodle and I have got this advanced functionality
this is a really newbie question: How can I output XML with Spring MVC,

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.