Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Parsing Files in .NET - Performance For Hire

I want the most performat way to read and parse a file.

Is it possible to read a file in .NET, but not load the entire file into memory? i.e. just load the file line by line as I parse the content of each row?

Does XmlTextReader load the entire file into memory or does it stream the file into memory as it reads the file?


1 Answers

You could use the ReadLine method of StreamReader Class:

string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");

while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
}

file.Close();

For XML files I would go with XMLTextReader. See this article on Dr. Dobb's Journal:

"Parsing XML Files in .NET Using C#: Five different parsing techniques are available in .NET, and each has its own advantages"

like image 154
splattne Avatar answered Dec 30 '25 16:12

splattne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!