XmlDocument.Validate不火的多个错误 [英] XmlDocument.Validate does not fire for multiple errors

查看:130
本文介绍了XmlDocument.Validate不火的多个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图验证对一个现有的XmlSchemaSet传入输入的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);
    }
}



中的代码验证的角度来看正常工作。然而,errorList仅捕获的第一个节点错误。它不捕获其他节点的错误。貌似事件被炒鱿鱼只有一次。如何做到这一点,请帮助。请注意,我收到的XmlDocument作为输入,因此不使用阅读器。

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.

推荐答案

这正是的 XmlDocument.Validate 方法。一旦它找到一个验证错误停止验证过程并返回错误。这样,用户以修复错误并再次验证。

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.

这行为是从Visual Studio错误列表不同。例如,如果你在代码中有一个语法错误,有时它会返回错误的100S。但实际上,你要修复只有在一个地方。所以,可以有两种利弊要看情况。不过,我不认为你可以很容易地得到所有的的XMLDocument验证错误,它在本质上是一种不同的方式。

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.

这篇关于XmlDocument.Validate不火的多个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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