用于单元测试的 .net Xml 比较器

发布于 2024-11-04 23:00:48 字数 261 浏览 0 评论 0原文

我有一些单元测试,需要确保方法生成的 XML 包含与预期 Xml 文档相同的元素/值。

我在 Java 中使用了 xmlunit ,尽管它们有 .net 版本,但它似乎不支持命名空间。 .net 中是否有其他替代方案可以实现此目的?

只要我可以比较 2 个 Xml 字符串并获得一个真/假结果来告诉我它们是否与所包含的数据匹配,我就很高兴......

I have a few unit tests where I need to make sure that XML generated by a method contains the same elements/values as an expected Xml document.

I used xmlunit in Java, and although they have a .net version it doesn't seem to support namespaces. Are there any alternatives within .net for doing this?

As long as I can just compare 2 Xml strings and get a true/false result to tell me if they match as far as the data contained is concerned I am happy...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃颗糖壮壮胆 2024-11-11 23:00:48

I've usually found that XNode.DeepEquals is sufficient for my needs. It's part of the BCL, so no download is required.

格子衫的從容 2024-11-11 23:00:48

尝试 Microsoft.XmlDiffPatch:

static public bool IsXmlEqual( XmlReader x1, XmlReader x2,
    bool IgnoreChildOrder, bool IgnoreComments, bool IgnorePI, bool IgnoreWhitespace,
    bool IgnoreNamespaces, bool IgnorePrefixes, bool IgnoreXmlDecl, bool IgnoreDtd
)
{
    XmlDiffOptions options = XmlDiffOptions.None;
    if (IgnoreChildOrder) options |= XmlDiffOptions.IgnoreChildOrder;
    if (IgnoreComments) options |= XmlDiffOptions.IgnoreComments;
    if (IgnorePI) options |= XmlDiffOptions.IgnorePI;
    if (IgnoreWhitespace) options |= XmlDiffOptions.IgnoreWhitespace;
    if (IgnoreNamespaces) options |= XmlDiffOptions.IgnoreNamespaces;
    if (IgnorePrefixes) options |= XmlDiffOptions.IgnorePrefixes;
    if (IgnoreXmlDecl) options |= XmlDiffOptions.IgnoreXmlDecl;
    if (IgnoreDtd) options |= XmlDiffOptions.IgnoreDtd;

    XmlDiff xmlDiff = new XmlDiff(options);
    bool bequal = xmlDiff.Compare(x1, x2, null);
    return bequal;
}

Try Microsoft.XmlDiffPatch:

static public bool IsXmlEqual( XmlReader x1, XmlReader x2,
    bool IgnoreChildOrder, bool IgnoreComments, bool IgnorePI, bool IgnoreWhitespace,
    bool IgnoreNamespaces, bool IgnorePrefixes, bool IgnoreXmlDecl, bool IgnoreDtd
)
{
    XmlDiffOptions options = XmlDiffOptions.None;
    if (IgnoreChildOrder) options |= XmlDiffOptions.IgnoreChildOrder;
    if (IgnoreComments) options |= XmlDiffOptions.IgnoreComments;
    if (IgnorePI) options |= XmlDiffOptions.IgnorePI;
    if (IgnoreWhitespace) options |= XmlDiffOptions.IgnoreWhitespace;
    if (IgnoreNamespaces) options |= XmlDiffOptions.IgnoreNamespaces;
    if (IgnorePrefixes) options |= XmlDiffOptions.IgnorePrefixes;
    if (IgnoreXmlDecl) options |= XmlDiffOptions.IgnoreXmlDecl;
    if (IgnoreDtd) options |= XmlDiffOptions.IgnoreDtd;

    XmlDiff xmlDiff = new XmlDiff(options);
    bool bequal = xmlDiff.Compare(x1, x2, null);
    return bequal;
}
勿忘心安 2024-11-11 23:00:48

关于 MSXML XMLDiff 需要记住的一点是,如果您要比较非常大的 XML 文档,请确保您的 XMLDiff.Algorithm 未设置为“Precise”,否则您可能会运行内存不足。默认情况下,其设置为“自动”,这是一个安全的选择,因为 API 将根据文件大小、检测到的差异数量和其他因素选择是使用“精确”还是“快速”。对于更倾向于技术的人来说,这是一个很好的阅读:

http://treepatch.sourceforge.net/report.pdf< /a>

Something to keep in mind about MSXML XMLDiff is that if you are comparing very large XML documents be sure your XMLDiff.Algorithm is not set to "Precise" or you will potentially run out of memory. By default its set to Auto which is a safe choice as the API will choose whether to use Precise or Fast based on the file size, number of detected differences and other factors. Here is a good read for the more technically inclined:

http://treepatch.sourceforge.net/report.pdf

中性美 2024-11-11 23:00:48

我过去曾使用过 MS 的 XMLDiff,但更喜欢使用 Beyond Compare 3,因为它具有更好的 GUI 和批处理功能(尽管没有 .NET API)。

为了进行测试,请使用 XNode.DeepEquals 或 InnerXML 来比较基于字符串的表示

I have used MS's XMLDiff is the past, but preferred to use Beyond Compare 3's as it has better GUI and batch processing feature (doenst have .NET API though).

for your testing, use XNode.DeepEquals or InnerXML to compare string based representation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文