From 85200e4ee2759ed51cad05287d1de462d94a2859 Mon Sep 17 00:00:00 2001 From: Mark Nuns Date: Thu, 23 Jan 2020 11:23:21 +0000 Subject: [PATCH 1/2] Added Unit Tests for Ignore XPath --- XmlDiffLib.Tests/UnitTest1.cs | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/XmlDiffLib.Tests/UnitTest1.cs b/XmlDiffLib.Tests/UnitTest1.cs index 34e8033..11a3d96 100644 --- a/XmlDiffLib.Tests/UnitTest1.cs +++ b/XmlDiffLib.Tests/UnitTest1.cs @@ -37,5 +37,52 @@ public void TestXmlDiff() Assert.AreEqual(diff.DiffNodeList.Count, 2); Assert.IsTrue(diff.DiffNodeList[1].Description.Contains("CompanyID")); } + + /// + /// GIVEN an xml structure with a node to be ignored + /// WHEN the xpath is specified that needs to be ignored + /// THEN comparedocument should return true + /// + [TestMethod] + public void TestXmlIgnoreXPathWithValue() + { + // Arrange + string fromXml = "FooBarFoo"; + string toXml = "FooFoo"; + + XmlDiff xmlDiff = new XmlDiff(fromXml, toXml); + + XmlDiffOptions xmlDiffOptions = new XmlDiffOptions(); + xmlDiffOptions.IgnoreXPaths.Add("Root/Node/IgnoreNode"); + + // Act + bool isSame = xmlDiff.CompareDocuments(xmlDiffOptions); + + // Assert + Assert.IsTrue(isSame); + } + + /// + /// GIVEN an xml structure with a node missing + /// WHEN no xpath to be ignored is not provided + /// THEN comparedocument should return false + /// + [TestMethod] + public void TestXmlIgnoreXPathWithoutValue() + { + // Arrange + string fromXml = "FooBarFoo"; + string toXml = "FooFoo"; + + XmlDiff xmlDiff = new XmlDiff(fromXml, toXml); + + XmlDiffOptions xmlDiffOptions = new XmlDiffOptions(); + + // Act + bool isSame = xmlDiff.CompareDocuments(xmlDiffOptions); + + // Assert + Assert.IsFalse(isSame); + } } } From 0e614e0ec1edd65a6814c0a53122535a4eb9c6dc Mon Sep 17 00:00:00 2001 From: Mark Nuns Date: Thu, 23 Jan 2020 11:27:07 +0000 Subject: [PATCH 2/2] Added Xml Path Ignore Logic --- XmlDiffLib/XmlDiff.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/XmlDiffLib/XmlDiff.cs b/XmlDiffLib/XmlDiff.cs index b30ec21..2cc2251 100644 --- a/XmlDiffLib/XmlDiff.cs +++ b/XmlDiffLib/XmlDiff.cs @@ -35,6 +35,7 @@ public enum IgnoreTextNodeOptions { XmlString, XmlInteger, XmlDouble, XmlDateTim public bool IgnoreChildOrder { get; set; } public bool IgnoreAttributes { get; set; } public HashSet IgnoreNodes { get; set; } + public HashSet IgnoreXPaths { get; set; } public bool IgnoreNamespace { get; set; } public bool IgnorePrefix { get; set; } public bool TrimWhitespace { get; set; } @@ -59,6 +60,7 @@ public XmlDiffOptions() MatchValueTypes = true; TwoWayMatch = false; IgnoreNodes = new HashSet(); + IgnoreXPaths = new HashSet(); IgnoreTextTypes = new HashSet(); MaxAttributesToDisplay = -1; } @@ -275,8 +277,11 @@ private List CompareNodes(XPathNavigator xmlFromNav, XPathNavigator do { - if (options.IgnoreNodes.Contains(xFrom.NodeType)) - continue; + if (options.IgnoreNodes.Contains(xFrom.NodeType) || + options.IgnoreXPaths.Contains(GetXPath(xFrom, false))) + { + continue; + } XmlDiffNode nodeInfo; if (!options.IgnoreChildOrder) @@ -495,7 +500,7 @@ private bool CompareTextValue(string fromValue, string toValue) return true; } - private string GetXPath(XPathNavigator nav) + private string GetXPath(XPathNavigator nav, bool addNodePosition = true) { Func addAttrib = (node) => @@ -525,7 +530,7 @@ private string GetXPath(XPathNavigator nav) if (string.IsNullOrEmpty(xNav.LocalName)) continue; string tempLabel = xNav.LocalName + addAttrib(xNav); - tempLabel += "[" + GetSiblingPosition(xNav) + "]"; + if (addNodePosition) { tempLabel += "[" + GetSiblingPosition(xNav) + "]"; } tempLabel += "/"; result.Insert(0, tempLabel);