Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore x path #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions XmlDiffLib.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,52 @@ public void TestXmlDiff()
Assert.AreEqual(diff.DiffNodeList.Count, 2);
Assert.IsTrue(diff.DiffNodeList[1].Description.Contains("CompanyID"));
}

/// <summary>
/// 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
/// </summary>
[TestMethod]
public void TestXmlIgnoreXPathWithValue()
{
// Arrange
string fromXml = "<Root><Node><SubNode>Foo</SubNode><IgnoreNode>Bar</IgnoreNode></Node><Node><SubNode><Element>Foo</Element></SubNode></Node></Root>";
string toXml = "<Root><Node><SubNode>Foo</SubNode></Node><Node><SubNode><Element>Foo</Element></SubNode></Node></Root>";

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

/// <summary>
/// GIVEN an xml structure with a node missing
/// WHEN no xpath to be ignored is not provided
/// THEN comparedocument should return false
/// </summary>
[TestMethod]
public void TestXmlIgnoreXPathWithoutValue()
{
// Arrange
string fromXml = "<Root><Node><SubNode>Foo</SubNode><IgnoreNode>Bar</IgnoreNode></Node><Node><SubNode><Element>Foo</Element></SubNode></Node></Root>";
string toXml = "<Root><Node><SubNode>Foo</SubNode></Node><Node><SubNode><Element>Foo</Element></SubNode></Node></Root>";

XmlDiff xmlDiff = new XmlDiff(fromXml, toXml);

XmlDiffOptions xmlDiffOptions = new XmlDiffOptions();

// Act
bool isSame = xmlDiff.CompareDocuments(xmlDiffOptions);

// Assert
Assert.IsFalse(isSame);
}
}
}
13 changes: 9 additions & 4 deletions XmlDiffLib/XmlDiff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public enum IgnoreTextNodeOptions { XmlString, XmlInteger, XmlDouble, XmlDateTim
public bool IgnoreChildOrder { get; set; }
public bool IgnoreAttributes { get; set; }
public HashSet<XPathNodeType> IgnoreNodes { get; set; }
public HashSet<string> IgnoreXPaths { get; set; }
public bool IgnoreNamespace { get; set; }
public bool IgnorePrefix { get; set; }
public bool TrimWhitespace { get; set; }
Expand All @@ -59,6 +60,7 @@ public XmlDiffOptions()
MatchValueTypes = true;
TwoWayMatch = false;
IgnoreNodes = new HashSet<XPathNodeType>();
IgnoreXPaths = new HashSet<string>();
IgnoreTextTypes = new HashSet<IgnoreTextNodeOptions>();
MaxAttributesToDisplay = -1;
}
Expand Down Expand Up @@ -275,8 +277,11 @@ private List<XmlDiffNode> 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)
Expand Down Expand Up @@ -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<XPathNavigator, string> addAttrib =
(node) =>
Expand Down Expand Up @@ -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);
Expand Down