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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:59:01+00:00 2026-05-22T12:59:01+00:00

There might be some very simple answer to this, but i am really stuck

  • 0

There might be some very simple answer to this, but i am really stuck on this one.

I have written some code that fetches quite a large (4GB+) xml file through ftp, reads it as a string and splits the document into smaller parts. Finally the smaller files are written to disc.

Everything works perfectly well on my developer machine, but when put into production, the script suddenly ends after reading through only a tenth of the file. No exceptions are thrown. Every single line of code is executed as expected. It just ends before going through the whole file. This is making me think that it is either some IIS or web.config settings that need to be tampered with.

Code is running inside the Umbraco CMS as a Custom user control. Server is a 2008 Windows machine running IIS.

Any ideas? This is the code:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Credentials = new NetworkCredential("anonymous", "x@y.z");
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Timeout = -1;
    request.KeepAlive = true;
    request.UsePassive = true;
    request.UseBinary = true;
    using (response = (FtpWebResponse)request.GetResponse())
    using (responseStream = response.GetResponseStream())
    using (StreamReader sr = new StreamReader(responseStream))
    {
      ReadStreamIntoNewRecord(fileName, sr, ref progress, ref result);
    }

The ReadStreamIntoNewRecord function looks like this:

private void ReadStreamIntoNewRecord(string fileName, StreamReader sr, int NumberOfRecordsPerBatch)
{
    string line = "";
    string record = "";
    int i = 0;  
    XDocument xdoc = new XDocument(new XElement("collection"));
    while (sr.Peek() >= 0)
    {
        line = sr.ReadLine();
        if (line.Contains("</record>"))
        {
            xdoc.Element("collection").Add(MakeRecordFromString(record + line));
            record = "";
            i++;
            if (i % NumberOfRecordsPerBatch == 0)
            {
                SaveRecordToFile(fileName, xdoc);
                xdoc = new XDocument(new XElement("collection"));
            }
        }
        else
        {
            record = record + line;
        }

    }
    SaveRecordToFile(fileName, xdoc);            
}
  • 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-22T12:59:02+00:00Added an answer on May 22, 2026 at 12:59 pm

    Wow, loading a 4GB file into a string in memory is a horrible idea. If it’s 4GB on disk as UTF-8 then it’ll be 8GB in memory since all .NE strings are UTF-16 in memory. Luckily, you’re not really doing that, you just said you were in the description.

    I believe you should change the while loop a little. As written it can be detecting an improper end of stream when there is really more data come in. Use this instead:

    while ((line = sr.ReadLine()) != null)
    {
        ...
    }
    

    Besides that, you would be much better off using either a simple StreamWriter or XmlTextWriter to save the file instead of XDocument. XDocument keeps the whole file in memory and is designed for easier traversal with Linq-to-Xml. You’re not using it and can benefit from a much lighter weight class.

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

Sidebar

Related Questions

There might be similar questions but I still have some parts that I couldn't
There might be a really simple solution to this question, but i just don't
I realise there might be similar questions but I couldn't find one that was
I guess there might be some overlapping with previous SO questions, but I could
There might be an easy solution here, but it seems to have me tripped
I am hoping that there might be an easy way to do this, I
This might seem as silly question but I am thinking there might be the
I have a mysql database that is currently running very slow. There are a
There's probably 5 or 6 SO posts that tangentially touch on this, but none
I know this question has kind-a started religious wars in past and there might

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.