JaiK
JaiK

Reputation: 377

How to display node name on mismatch and ignore a namsepace in XMLDiff

I am using the below code XmlDiff to compare two xml files.

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);

The xml file generated after the comparison is :

<?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>

I wanted to get the node name for "USA", which is "countryName", and also wanted to ignore 7909, which is a timestamp. The XML file has many nodes and that's the reason I would like to print "CountryName" node to see the value.

Also node with value "7909" belongs to a namespace called tig as in xmlns:tig="..." and I don't mind ignoring everything with tig namspace in the document. However I am not sure how to suppress it during comparison.

<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>

Upvotes: 1

Views: 236

Answers (0)

Related Questions