XmlDocument.Validate 不会因多个错误而触发

发布于 2024-12-05 09:14:27 字数 1005 浏览 0 评论 0原文

我正在尝试根据现有的 XmlSchemaSet 验证传入的输入 xmlDocument。以下是代码:

public class ValidateSchemas
{
    private bool _isValid = true;

    public List<string> errorList = new List<string>();

    public bool ValidateDocument(XmlDocument businessDocument)
    {
       XmlSchemaSet schemaSet = SchemaLoader.Loader();
       bool isValid = Validate(businessDocument, SchemaLoader._schemaSet);
       return isValid;
    }

    public bool Validate(XmlDocument document, XmlSchemaSet schema)
    {
        ValidationEventHandler eventHandler = new ValidationEventHandler(HandleValidationError);
            document.Schemas = schema;
            document.Validate(eventHandler);
            return _isValid;
    }

    private  void HandleValidationError(object sender, ValidationEventArgs ve)
    {
        _isValid = false;   errorList.Add(ve.Message);
    }
}

从验证的角度来看,该代码运行良好。然而,errorList 仅捕获第一个节点错误。它不捕获其他节点错误。看起来该事件只被解雇一次。如何实现这一点,请帮忙。请注意,我将 xmldocument 作为输入,因此不使用阅读器。

I am trying to validate an incoming input xmlDocument against a an existing XmlSchemaSet. Following is the code:

public class ValidateSchemas
{
    private bool _isValid = true;

    public List<string> errorList = new List<string>();

    public bool ValidateDocument(XmlDocument businessDocument)
    {
       XmlSchemaSet schemaSet = SchemaLoader.Loader();
       bool isValid = Validate(businessDocument, SchemaLoader._schemaSet);
       return isValid;
    }

    public bool Validate(XmlDocument document, XmlSchemaSet schema)
    {
        ValidationEventHandler eventHandler = new ValidationEventHandler(HandleValidationError);
            document.Schemas = schema;
            document.Validate(eventHandler);
            return _isValid;
    }

    private  void HandleValidationError(object sender, ValidationEventArgs ve)
    {
        _isValid = false;   errorList.Add(ve.Message);
    }
}

The code works fine from a validation perspective. However the errorList captures only the first node error. It does not capture the other node errors. Looks like the event is getting fired only once. How to accomplish this, please help. Please note I am getting xmldocument as input , hence not using a reader.

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

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

发布评论

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

评论(1

撩起发的微风 2024-12-12 09:14:27

这正是 XmlDocument.Validate 方法的预期行为。一旦发现验证错误,它就会停止验证过程并返回错误。因此,用户必须修复该错误并再次验证。

此行为与 Visual Studio 错误列表不同。例如,如果代码中有一个语法错误,有时它会返回数百个错误。但实际上你只需在一处修复一个即可。所以,有利有弊,要根据具体情况而定。但是,我认为您无法轻松获得 XMLDocument 的所有验证错误,它本质上以不同的方式工作。

That's exactly the expected behavior of XmlDocument.Validate method. Once it finds a validation error it stops validate process and returns the error. So, the user has to fix that error and validate again.

This behavior is different from the Visual studio error list. For example, if you have a single syntax error in the code sometimes it returns 100s of errors. But actually you have to fix only one at one place. So, there can be both pros and cons depends on the circumstance. However, I don't think you could easily get all the validation errors for a XMLDocument, it works in a different way inherently.

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