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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:05:14+00:00 2026-06-16T16:05:14+00:00

I’m trying to read through a 2.5GB XML file and delete certain nodes, lets

  • 0

I’m trying to read through a 2.5GB XML file and delete certain nodes, lets say, the “CD” elements and the “DVD” elements. Currently I’m doing something like this:

using (XmlReader reader = XmlReader.Create("file.xml"))
{
    DeleteElements(reader.ReadElements("CD"));
    DeleteElements(reader.ReadElements("DVD")); // reader returns 0 elements
}

Note: DeleteElements just loops these elements and removes them from the document, but that’s mostly unimportant for the purposes of this question.

Currently I find that no “DVD” element are retrieved. If you’ve worked with XmlReader that much before, I’m sure you can figure the cause of the problem here: after the reader reads the document for “CD” nodes, the reader doesn’t find any “DVD” elements because the reader is at the end of the document.

Considering the large size of the XML file, and the number of elements I’m trying to retrieve, I can’t load the entire document into memory because you’d get a OutOfMemoryException – this means no XDocument or XPathDocument goodness.

Is there any way to get XmlReader to return both “CD” and “DVD” as it reads through the document? Loading the document initially is quite time consuming, so I don’t want to do this multiple times. Something awesome like reader.ReadElements("DVD|CD") would be sweet.

  • 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-16T16:05:15+00:00Added an answer on June 16, 2026 at 4:05 pm

    XmlReader is a forward only xml parser. If there would be a ReadElements method, it would run the reader to the end, and then there are no more DVD elements. So you would have to run twice over your file.

    Basic usage of XmlReader:

    using (XmlReader reader = XmlReader.Create("input.xml")) {
      while (reader.Read()) {
        switch (reader.NodeType) {
        case XmlNodeType.Element:
          switch (reader.Name) {
          case "CD":
            // do something with a CD
            break;
          case "DVD":
            // do something with a DVD
            break;
          default:
            // do something with all other elements
            break;
          }
          break;
        }
      }
    }
    

    What are you doing in the DeleteElements method? You probably need to create a XmlWriter for a new temp file, then write all elements except the one you’d like to delete to the temp file, and at least replace the original file with the temp file.

    This way you have one loop over all elements, including the deletion (exclusion) of some.

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm parsing an XML file, the creators of it stuck in a bunch social
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I'm using an ASP request returning a XML file containing some latin characters. By

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.