如何使 XMLUNIT 的 WithNodeFilter 动态 C#

How to make XMLUNIT's WithNodeFilter dynamic C#

我正在开发一个比较 XML 个文件的应用程序。用户可以输入他们想要在比较中排除的节点列表。为了进行比较,我使用 XMLUNIT。我需要动态添加用户输入。

下面的代码有效,但对用户输入不是动态的:

private bool Example1(ISource control, ISource test)
{
    var excludedNodes = new List<string> { "UserInput1", "UserInput2", "UserInput3" };
    var diff = DiffBuilder
        .Compare(control)
        .WithTest(test)
        .WithNodeFilter(x => !(x.Name.Equals(excludedNodes[0]) || x.Name.Equals(excludedNodes[1]) || x.Name.Equals(excludedNodes[2])))
        .Build();
    var hasDifferences = diff.HasDifferences();
    return hasDifferences;
}

我动态创建上面的尝试:

private bool Example2(ISource control, ISource test)
{
    var excludedNodes = new List<string> { "UserInput1", "UserInput2", "UserInput3" };
    var diffBuilder = DiffBuilder
        .Compare(control)
        .WithTest(test);
    foreach (var excludedNode in excludedNodes)
    {
        diffBuilder.WithNodeFilter(x => !x.Name.Equals(excludedNode));
    }
    var diff = diffBuilder.Build();
    var hasDifferences = diff.HasDifferences();
    return hasDifferences;
}

看来像我在示例 2 中那样将 "WithNodeFilter" 链接在一起是行不通的。

我无法确定编译,但我认为您需要将其视为检查排除的节点是否具有您的 node.Name - 而不是检查名称并将其与每个排除的节点进行比较。

private bool Example1(ISource control, ISource test)
{
    var excludedNodes = new List<string> { "UserInput1", "UserInput2", "UserInput3" };
    var diff = DiffBuilder
        .Compare(control)
        .WithTest(test)
        .WithNodeFilter(x => !excludedNodes.contains(x.Name))
        .Build();
    var hasDifferences = diff.HasDifferences();
    return hasDifferences;
}