如何使XMLUNIT的WithNodeFilter动态C#

时间:2019-03-20 13:22:00

标签: c# xml dynamic xmlunit

我正在开发一个比较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;
}

似乎无法像在example2中那样将“ WithNodeFilter”链接在一起。

1 个答案:

答案 0 :(得分:0)

我不确定是否可以编译,但是我认为您需要将其视为检查排除的节点是否具有您的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;
}