我正在尝试针对现有的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
从验证的角度来看,代码运行良好。但是 errorList 仅捕获第一个节点错误。它不会捕获其他节点错误。看起来该事件仅被触发一次。如何做到这一点,请帮助。请注意,我将 xmldocument 作为输入,因此没有使用阅读器。