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