从NuGet安装免费的XMLDiffMerge包.该软件包本质上是Microsoft 的XML Diff和Patch GUI Tool的重新打包版本.
true如果两个XML文件相同,则此函数返回,忽略注释,空格和子顺序.作为奖励,它还可以解决差异(参见differences函数中的内部变量).
/// <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;
}
Run Code Online (Sandbox Code Playgroud)
以下是我们如何调用该函数:
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).")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1748 次 |
| 最近记录: |