在.NET中读取和解析文件 - 租用性能

3 .net performance file

我想要最经典的方式来读取和解析文件.

是否可以在.NET中读取文件,但不能将整个文件加载到内存中?即只是在解析每行的内容时逐行加载文件?

XmlTextReader是否将整个文件加载到内存中,或者在读取文件时将文件流式传输到内存中?

spl*_*tne 10

您可以使用StreamReader类的ReadLine方法:

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();
Run Code Online (Sandbox Code Playgroud)

对于XML文件,我会使用XMLTextReader.请参阅Dobb博士期刊上的这篇文章:

" 使用C#在.NET中解析XML文件: .NET中提供了五种不同的解析技术,每种技术都有自己的优势"