删除xml文件中空行的最有效方法? [英] Most efficient way of removing blank lines in a xml file?

查看:442
本文介绍了删除xml文件中空行的最有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的重复节点删除程序,其代码如下:

I have a very basic duplicate node removing program whose code is given below

XDocument xdoc=XDocument.Load(@"D:\test\12345.XML",LoadOptions.PreserveWhitespace);
xdoc.Descendants("kwd")
				.GroupBy(g => (string)g.Value.ToLower())
				.Where(g => g.Count() > 1)
				.SelectMany(g => g.Skip(1))
				.Remove();
xdoc.Save(@"D:\test\12345.XML",SaveOptions.DisableFormatting);

我想删除所有空行这个过程之后的文件(如果有的话)。

I want to remove all blank lines in the file(if any) after this process.

所以,我尝试了几种方法(如下所示),但不确定哪一种更快更有效?

So, I tried a few ways of doing it(given below) but not sure which one of them is faster and more efficient?

1)

var subjectString = File.ReadAllText(@"D:\test\12345.XML");
var resultString = Regex.Replace(subjectString, @"[\r\n]*^\s*$[\r\n]*", "", RegexOptions.Multiline);
File.WriteAllText(@"D:\test\12345.XML",resultString);

2)

File.WriteAllLines(@"D:\test\12345.XML", File.ReadAllLines(@"D:\test\12345.XML")
.Where(l => !string.IsNullOrWhiteSpace(l)));

and 3)

var tempFileName = Path.GetTempFileName();
try
{
	using (var streamReader = new StreamReader(@"D:\test\12345.XML"))
		using (var streamWriter = new StreamWriter(tempFileName))
	{
		string line;
		while ((line = streamReader.ReadLine()) != null)
		{
			if (!string.IsNullOrWhiteSpace(line))
				streamWriter.WriteLine(line);
		}
	}
	File.Copy(tempFileName, @"D:\test\12345.XML", true);
}
finally
{
	File.Delete(tempFileName);
}

任何人都可以帮助我。

此外,如果有人有任何其他实现此目的的过程请显示我...

Also, if anyone has any other process of achieving this please show me...

推荐答案

如果文件很大,可能最好的方法是3号,因为它不需要加载整个内容内存中的文件。但我建议改进:不要在临时文件夹中分配临时文件,而是在同一文件夹
中分配一个文件,其中包含原始文件。然后,将新文件删除原始文件并将
重命名作为原始文件,而不是复制原始文件。这样可以节省在磁盘上执行文件整个内容的一个副本。
If the files are large, probably the best way is number 3, since it doesn't require loading the whole content of the file in memory. But I suggest an improvement: instead of allocating a temporary file in the temp folder, assign a file in the same folder where you have the original file. Then, instead of copying the new file over the original, just delete the original and rename the new file as the original. This saves performing on disk one copy of the whole content of the file.


这篇关于删除xml文件中空行的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆