本文介绍了删除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...
推荐答案
这篇关于删除xml文件中空行的最有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!