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

  • Home
  • SEARCH
  • 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 673171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:33:58+00:00 2026-05-14T00:33:58+00:00

I have a method which returns some xml in a memory stream private MemoryStream

  • 0

I have a method which returns some xml in a memory stream

private MemoryStream GetXml()
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;

    using (MemoryStream memoryStream = new MemoryStream())
    {
        using (XmlWriter writer = XmlWriter.Create(memoryStream, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteStartElement("element");
            writer.WriteString("content");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }

        return memoryStream;
    }
}

In this example the format of the xml will be:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <element>content</element>
</root>

How can I insert a new element under the root e.g.:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <element>content</element>
  ----->New element here <------
</root>

EDIT:

Also please suggest the most efficient method as returning a MemoryStream may not be the best solution.

The final xml will be passed to a custom HttpHandler so what are the best options for writing the output?

context.Response.Write vs context.Response.OutputStream

  • 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-14T00:33:59+00:00Added an answer on May 14, 2026 at 12:33 am

    Your first issue is that you don’t have XML – you have a stream of bytes which can be loaded into an XDocument or XmlDocument. Do you really need to output to a stream instead of directly into an XML Document?

    BTW, I think you need to lose the using block around the MemoryStream. You won’t be able to use the stream for much after it’s been disposed.

    public void AppendElement()
    {
        XDocument doc;
        using(MemoryStream stream = GetXml())
        {
            using(var sr = new StreamReader(stream))
            {
                doc = XDocument.Load(sr);
            }
        }
        if(doc.Root != null)
        {
            doc.Root.Add(new XElement("Whatever"));
        }
    }
    
    private static MemoryStream GetXml()
    {
        var settings = new XmlWriterSettings {Indent = true};
    
        var memoryStream = new MemoryStream();
        using (XmlWriter writer = XmlWriter.Create(memoryStream, settings))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteStartElement("element");
            writer.WriteString("content");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
    
        return memoryStream;
    }
    

    For efficiency, try to work directly with XDocument. You seem to want to use XmlWriter, so here’s how to do it (not tested):

    public void AppendElement()
    {
        XDocument doc = GetXml();
        if(doc.Root != null)
        {
            doc.Root.Add(new XElement("Whatever"));
        }
    }
    
    private static XDocument GetXml()
    {
        var doc = new XDocument();
    
        using (XmlWriter writer = doc.CreateWriter())
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");
            writer.WriteStartElement("element");
            writer.WriteString("content");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
    
        return doc;
    }
    

    Update:

    You can write the XML with

    doc.Save(HttpContext.Response.Output);
    

    Again, I can’t test it now, but try this:

    private static XDocument GetXml()
    {
        return new XDocument(
            new XElement(
                "root", 
                new XElement(
                    "element", 
                    "content")));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 345k
  • Answers 345k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you see Drawing Applications / Libraries like Qt (QPainter),… May 14, 2026 at 6:00 am
  • Editorial Team
    Editorial Team added an answer Yep here is a link explaning the features of the… May 14, 2026 at 6:00 am
  • Editorial Team
    Editorial Team added an answer This is a tough one. Automated testing tools and frameworks… May 14, 2026 at 6:00 am

Related Questions

EDIT: See my working code in the answers below. In brief: I have a
I have a function which returns the inner text of an xml element. It
I'm designing an API for a web service and I can't decide between using
A project of mine involves a flash movie (.swf) in a webpage, where the
I'm currently working on posting a file from a C# application to an image

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.