Q:
Я хочу разорвать цикл, если в событии было обнаружено какое-то исключение.
Код:
XmlDocument x = new XmlDocument();
x.Load(targetFileName);
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += Handler;
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null,System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
using (StreamReader str_reader = new StreamReader(targetFileName))
{
using (XmlReader validatingReader = XmlReader.Create(str_reader, settings))
{
while (validatingReader.Read())
{
//I wanna to break this loop if there is some caught error .
}
}
}
private static void Handler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
{
try
{
throw new Exception(
String.Format("Line: {0}, Position: {1} \"{2}\"",
e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
}
catch (Exception ee)
{
Common.ErrMappingForInformix.WriteLog(ee.Message);
ScheduleForm sf = new ScheduleForm();
sf.ShowStatus("Error,Invalid xml file", "error", "", 1);
}
}
}
# Редактировать 1:
XmlDocument x = new XmlDocument();
x.Load(targetFileName);
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationEventHandler += (senderValidation, ee) =>
{
if (ee.Severity == XmlSeverityType.Error || ee.Severity == XmlSeverityType.Warning)
{
try
{
this.validationFailed = true;
throw new Exception(
String.Format("Line: {0}, Position: {1} \"{2}\"",
ee.Exception.LineNumber, ee.Exception.LinePosition, ee.Exception.Message));
}
catch (Exception ex)
{
Common.ErrMappingForInformix.WriteLog(ex.Message);
this.ShowStatus("Error", "error", "", 1);
}
}
};
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
using (StreamReader str_reader = new StreamReader(targetFileName))
{
using (XmlReader validatingReader = XmlReader.Create(str_reader, settings))
{
while (validatingReader.Read())
{
//Loop through the document
if (validationFailed)
{
break;
}
}
}
}
Он прерывает цикл, но не выполняет обработчик событий.