如何使XMLUNIT的WithNodeFilter动态C# [英] How to make XMLUNIT's WithNodeFilter dynamic C#

查看:172
本文介绍了如何使XMLUNIT的WithNodeFilter动态C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an app which compares XML files. The user may input a list of nodes in which they want to exclude in the comparison. For my comparison I am using XMLUNIT. I need to dynamically add the user input.

以下代码有效,但对于用户输入而言不是动态的:

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"链接在一起.

It appears that chaining together "WithNodeFilter" like I did in example2 does not work.

推荐答案

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

I can't compile to be sure, but I think you need to think of it as checking if the excluded nodes has your node.Name - instead of checking the name and comparing it to each excluded node.

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

这篇关于如何使XMLUNIT的WithNodeFilter动态C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆