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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:19:58+00:00 2026-06-02T04:19:58+00:00

I am trying to write an XML file but it is too large to

  • 0

I am trying to write an XML file but it is too large to store in memory, thus I want to write to it directly to disk. I have tried using XmlWriter but there is not functionality to enable me to append to the end of the file, hence I am willing to resort to writing the XML raw using a regular file writer.

Does anyone know of any file writing classes that enable me to write straight to disk and which enable me to overwrite positions inside the file?

The reason is that I need to be able to write over the closing of the root element so that I may append another bit of information, but also be able to read the XML file when needed. For example, if I had the following XML:

<elements>
  <element>
  </element>
</elements>

If I wanted to read this, I could, but if I want to write to it I must first delete the </elements> tag, append another element, and then append the closing tag again.

Thanks for any help.

  • 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-02T04:20:00+00:00Added an answer on June 2, 2026 at 4:20 am

    You can use an XmlTextWriter.

    Just open the file for writing, seek back to the start of the end element, and then append any new elements you want with the XmlTextWriter. To close the file, simply write the raw text for the end element to make the document complete and you’re done.

    Here’s a quick and dirty example.

    Starting with XML like this:

    <?xml version="1.0" encoding="utf-8"?>
    <DocumentElement>
        <FirstElem/>
    </DocumentElement>
    

    You can open it and append an element like this:

    using (FileStream f = new FileStream(@"D:\a.xml", FileMode.OpenOrCreate, FileAccess.Write))
    {
        f.Seek(-("</DocumentElement>\n".Length), SeekOrigin.End);
        using (XmlTextWriter x = new XmlTextWriter(f, Encoding.UTF8))
        {
            x.WriteStartElement("Another");
            x.WriteAttributeString("attr", "value");
            x.WriteEndElement();
    
            // Close the file with a new terminating end-element
            x.WriteRaw("\r\n</DocumentElement>\r\n");
        }
    }
    

    And the result is:

    <?xml version="1.0" encoding="utf-8"?>
    <DocumentElement>
        <FirstElem/>
    <Another attr="value" />
    </DocumentElement>
    

    You may not get the indentation perfect etc, but it’s valid XML. This is exactly what you’d do if writing xml as raw text to the file – but you might as well leverage the XML writer to do the formatting for you.

    I’d also agree with some of the comments – it will be very beneficial to use a schema for your xml that minimises the size. Turn off indentation. Use the shortest element and attribute names you can. And if you are working on leaf elements, storing data as attributes rather than cdata will save room (<element>data</element> is more expensive than <element val="data"/> and this can be compressed further to <e v="data"/> – almost half the original size)

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

Sidebar

Related Questions

I'm trying to write on xml file. but my server have DOM as disabled.
I am trying to write a large XML file(in excess of 1.5M) to a
I'm trying to write to an XML file using Builder but cannot seem to
I am trying to write a xml file using xercesc::DOMWriter in c++. But I
I'm trying to write an xml file with utf-8 encoded data using ElementTree like
I am trying to write a servlet that will send a XML file (xml
I'm trying to write a client-server application in Java with an XML-based protocol. But
I am trying to write a query expression to parse an XML tree, but
I'm trying to write xml data with XmlLite on buffer but couldn't got any
I'm trying to write an XML file with UTF-8 encode, and the original string

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.