比较XML文件以查看C#是否存在差异 [英] Comparing XML Files to see if there is a difference C#
本文介绍了比较XML文件以查看C#是否存在差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个控制台应用程序,允许某人输出和XML文件.我需要的是能够在每次运行时检查XML文件是否已更改以及是否已更改?有什么想法吗?
I am creating a console app that allows for someone to output and XML file. What I need is to be able to check every time it is run whether or not the XML file has changed and if so to increment it? Any ideas?
XmlDocument doc = new XmlDocument();
if (doc.LastChild.Value == lsat saved doc)
{
// My code here.
}
推荐答案
您可以尝试创建xml文件的MD5哈希,并检查哈希是否相等.其余的您都知道.
You could try creating an MD5 hash of the xml file and check if the hashes are equal. You know the rest.
有时候,MD5哈希可能导致相同的结果.我将执行一些检查,这些检查将逐渐花费更多时间:
0)查看文件大小是否相同
1)如果大小相同,则将文件作为文本字符串加载,消除空格,并进行简单的相等比较.
There are times when a MD5 hash could result in an identical result. I would perform several checks that took gradually more time:
0) See if the file sizes are the same
1) If they are the same size, load the files as text strings, eliminate the white space, and do a simple equality comparison.
最后我还好用了
if (doc.InnerXml.Length != doc2.InnerXml.Length)
{
UpdateLatestVersion(newVersion);
}
这篇关于比较XML文件以查看C#是否存在差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文