如何在不匹配时显示节点名称并忽略XMLDiff中的namsepace

时间:2015-01-16 04:31:39

标签: xml c#-4.0 xml-parsing xmldiff

我使用以下代码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中名为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>

0 个答案:

没有答案