我正在使用下面的代码 XmlDiff 来比较两个 xml 文件。
XmlReader reader1 = XmlReader.Create(new StringReader(file1));
XmlReader reader2 = XmlReader.Create(new StringReader(file2));
string diffFile = "D:\\XMLDiffCompare\\XmlDiffFilename.xml";
StringBuilder differenceStringBuilder = new StringBuilder();
FileStream fs = new FileStream(diffFile, FileMode.Create);
XmlWriter diffGramWriter = XmlWriter.Create(fs);
// XmlTextWriter diffgram = new XmlTextWriter(Console.Out);
// diffgram.Formatting = Formatting.Indented;
XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder );
bool bIdentical = xmldiff.Compare(file1, file2, false, diffGramWriter);
比较后生成的xml文件为:
<?xml version="1.0" encoding="UTF-8"?>
<xd:xmldiff xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff" version="1.0" srcDocHash="12061993843110569490" options="IgnoreNamespaces " fragments="no">
<xd:node match="2">
<xd:node match="1">
<xd:node match="2">
<xd:node match="2">
<xd:node match="1">
<xd:change match="1">USA</xd:change>
</xd:node>
</xd:node>
</xd:node>
</xd:node>
<xd:node match="2">
<xd:node match="1">
<xd:change match="1">7909</xd:change>
</xd:node>
</xd:node>
</xd:node>
</xd:xmldiff>
我想获取 的节点名称"USA"
,也就是"countryName"
,并且还想忽略7909
,这是一个时间戳。XML 文件有很多节点,这就是我想打印"CountryName"
节点以查看值的原因。
此外,值为“7909”的节点属于名为tig
in的命名空间xmlns:tig="..."
,我不介意忽略tig
文档中包含 namspace 的所有内容。但是我不确定在比较过程中如何抑制它。
<ContactInfo>
<PersonName>
<FormattedName>My Name</FormattedName>
<GivenName>Test first Name</GivenName>
<FamilyName>Test Last Name</FamilyName>
</PersonName>
<ContactMethod>
<WhenAvailable>anytime</WhenAvailable>
<PostalAddress type="undefined">
<CountryCode>USA</CountryCode>
<Region>region</Region>
<DeliveryAddress>
<AddressLine>Address to get </AddressLine>
</DeliveryAddress>
</PostalAddress>
</ContactMethod>
</ContactInfo>