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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:58:02+00:00 2026-06-12T11:58:02+00:00

What I have: A large XML file @ nearly 1 million lines worth of

  • 0

What I have:

A large XML file @ nearly 1 million lines worth of content. Example of content:

<etc35yh3 etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>
<etc123 etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>
<etc15y etc="numbers" etc234="a" etc345="date"><something><some more something></some more something></something></etc123>

^ repeat that by 900k or so lines (content changing of course)

What I need:

Search the XML file for "<etc123". Once found move (write) that line along with all lines below it to a separate XML file.

Would it be advisable to use a method such as File.ReadAllLines for the search portion? What would you all recommend for the writing portion. Line by line is not an option as far as I can tell as it would take much too long.

  • 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-12T11:58:03+00:00Added an answer on June 12, 2026 at 11:58 am

    To quite literaly discard the content above your search string, I would not use File.ReadAllLines, as it would load the entire file into memory. Try File.Open and wrap it in a StreamReader. Loop on StreamReader.ReadLine, then start writing to a new StreamWriter, or do a byte copy on the underlying filestream.

    An example of how to do so with StreamWriter/StreamReader alone is listed below.

    //load the input file
    //open with read and sharing
    using (FileStream fsInput = new FileStream("input.txt", 
        FileMode.Open, FileAccess.Read, FileShare.Read)) 
    {
        //use streamreader to search for start
        var srInput = new StreamReader(fsInput);
        string searchString = "two";
        string cSearch = null;
        bool found = false;
        while ((cSearch = srInput.ReadLine()) != null)
        {
            if (cSearch.StartsWith(searchString, StringComparison.CurrentCultureIgnoreCase)
            {
                found = true;
                break;
            }
        }
        if (!found)
            throw new Exception("Searched string not found.");
    
        //we have the data, write to a new file
        using (StreamWriter sw = new StreamWriter(
            new FileStream("out.txt", FileMode.OpenOrCreate, //create or overwrite
                FileAccess.Write, FileShare.None))) // write only, no sharing
        {
            //write the line that we found in the search
            sw.WriteLine(cSearch);
    
            string cline = null;
            while ((cline = srInput.ReadLine()) != null)
                sw.WriteLine(cline);
        }
    }
    
    //both files are closed and complete
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large xml file (contains about few million records ) and need
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have a large XML file (many MBs) that I cannot afford to download
I have a large xml file that looks like this: 20120124 07:30:15.301, saving to
I have a very large XML file which has like 40000 data, and when
I want to parse a large XML file and I have two options: Perl
I have a request that returns a large xml file. I have the file
I have to send a large amount of xml data through sockets. Example of
I have a large XML file (converted to JSON) that consists of multiple repeating

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.