0
我有XML文件,它有2个XSD错误。但ValidationEventHandler只触发一次,并且仅在我纠正第一个错误时才显示第二个错误。 这是我的代码:ValidationEventHandler只触发一次?
public static void ValidateXml(string xml, string xsd)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += settings_ValidationEventHandler;
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(xsd));
if (settings.Schemas.Count > 0)
{
using (XmlReader reader = XmlReader.Create(xml, settings))
{
while (reader.Read())
{
}
}
}
}
private static void settings_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
Console.WriteLine("Validation Error Message: {0}", e.Message);
Console.WriteLine("Validation Error Severity: {0}", e.Severity);
if (e.Exception != null)
{
Console.WriteLine("Validation Error Line Number: {0}",
e.Exception.LineNumber);
Console.WriteLine("Validation Error Line Position: {0}",
e.Exception.LinePosition);
}
}
XmlReader继续读取当前文档直到结束,但忽略错过的元素。我必须生成一份报告,显示文件中的所有错误。 – Wachburn
也许你可以尝试验证方法 – rgargente