检查两个XML文件是在C#中的一样吗? [英] Check if two XML files are the same in C#?
本文介绍了检查两个XML文件是在C#中的一样吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检查,看看是否两个XML文件是在C#中的一样吗?
How do I check to see if two XML files are the same in C#?
我想忽略在XML文件中的注释。
I want to ignore comments in the XML file.
推荐答案
安装免费的 XMLDiffMerge套餐的NuGet 。这个包在本质上是 XML差异和微软补丁GUI工具的重新包装版。
Install the free XMLDiffMerge package from NuGet. This package is essentially a repackaged version of the XML Diff and Patch GUI Tool from Microsoft.
这个函数返回真
如果两个XML文件是相同的,忽略的意见,白色的空间,和子序列。作为奖励,它也可以出差异(见的差异
在函数内部变量)。
This function returns true
if two XML files are identical, ignoring comments, white space, and child order. As a bonus, it also works out the differences (see the internal variable differences
in the function).
/// <summary>
/// Compares two XML files to see if they are the same.
/// </summary>
/// <returns>Returns true if two XML files are functionally identical, ignoring comments, white space, and child
/// order.</returns>
public static bool XMLfilesIdentical(string originalFile, string finalFile)
{
var xmldiff = new XmlDiff();
var r1 = XmlReader.Create(new StringReader(originalFile));
var r2 = XmlReader.Create(new StringReader(finalFile));
var sw = new StringWriter();
var xw = new XmlTextWriter(sw) { Formatting = Formatting.Indented };
xmldiff.Options = XmlDiffOptions.IgnorePI |
XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreComments |
XmlDiffOptions.IgnoreWhitespace;
bool areIdentical = xmldiff.Compare(r1, r2, xw);
string differences = sw.ToString();
return areIdentical;
}
下面是我们如何调用该函数:
Here is how we call the function:
string textLocal = File.ReadAllText(@"C:\file1.xml");
string textRemote = File.ReadAllText(@"C:\file2.xml");
if (XMLfilesIdentical(textLocal, textRemote) == true)
{
Console.WriteLine("XML files are functionally identical (ignoring comments).")
}
这篇关于检查两个XML文件是在C#中的一样吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文