EDN Admin
Well-known member
I have an XML string and a Schema loaded up and passed into a function. I have it validating the XML against the schema correctly, however it always stops at the first validation error. I wish to find all the errors so we are able to report all the errors
at once, rather than have people submit/fix/resubmit the messages.
the code is as follows
<pre class="prettyprint" style=" public static void ValidateAgainstSchema(string XMLSourceDocument, XmlSchemaSet validatingSchemas)
{
if (validatingSchemas == null)
{
throw new ArgumentNullException("In ValidateAgainstSchema: No schema loaded.");
}
string errorHolder = string.Empty;
ValidationHandler handler = new ValidationHandler();
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(handler.HandleValidationError);
settings.Schemas.Add(validatingSchemas);
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader srStringReader = new StringReader(XMLSourceDocument);
using (XmlReader validatingReader = XmlReader.Create(srStringReader, settings))
{
while (validatingReader.Read()) { }
}
if (handler.MyValidationErrors.Count > 0)
{
foreach (String messageItem in handler.MyValidationErrors)
{
errorHolder += messageItem;
}
throw new XmlSchemaValidationException(errorHolder);
}
}[/code]
<br/>
and the validation handler class is such:
<pre class="prettyprint" style=" public class ValidationHandler
{
private IList<string> myValidationErrors = new List<String>();
public IList<string> MyValidationErrors { get { return this.myValidationErrors; } }
public void HandleValidationError(object sender, ValidationEventArgs ve)
{
if (ve.Severity == XmlSeverityType.Error || ve.Severity == XmlSeverityType.Warning)
{
this.myValidationErrors.Add(
String.Format(
Environment.NewLine + "Line: {0}, Position {1}: "{2}"",
ve.Exception.LineNumber,
ve.Exception.LinePosition,
ve.Exception.Message)
);
}
}
}[/code]
<br/>
Is there any way I can do what I want? Its obviously not working as is, but is there something obvious Im missing? Like I said, the first error in an XML document always comes through fine and as expected, but then the HandleValidationError event handler
is never called for subsequent errors.
Thanks,
<br/>
View the full article
at once, rather than have people submit/fix/resubmit the messages.
the code is as follows
<pre class="prettyprint" style=" public static void ValidateAgainstSchema(string XMLSourceDocument, XmlSchemaSet validatingSchemas)
{
if (validatingSchemas == null)
{
throw new ArgumentNullException("In ValidateAgainstSchema: No schema loaded.");
}
string errorHolder = string.Empty;
ValidationHandler handler = new ValidationHandler();
XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(handler.HandleValidationError);
settings.Schemas.Add(validatingSchemas);
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
StringReader srStringReader = new StringReader(XMLSourceDocument);
using (XmlReader validatingReader = XmlReader.Create(srStringReader, settings))
{
while (validatingReader.Read()) { }
}
if (handler.MyValidationErrors.Count > 0)
{
foreach (String messageItem in handler.MyValidationErrors)
{
errorHolder += messageItem;
}
throw new XmlSchemaValidationException(errorHolder);
}
}[/code]
<br/>
and the validation handler class is such:
<pre class="prettyprint" style=" public class ValidationHandler
{
private IList<string> myValidationErrors = new List<String>();
public IList<string> MyValidationErrors { get { return this.myValidationErrors; } }
public void HandleValidationError(object sender, ValidationEventArgs ve)
{
if (ve.Severity == XmlSeverityType.Error || ve.Severity == XmlSeverityType.Warning)
{
this.myValidationErrors.Add(
String.Format(
Environment.NewLine + "Line: {0}, Position {1}: "{2}"",
ve.Exception.LineNumber,
ve.Exception.LinePosition,
ve.Exception.Message)
);
}
}
}[/code]
<br/>
Is there any way I can do what I want? Its obviously not working as is, but is there something obvious Im missing? Like I said, the first error in an XML document always comes through fine and as expected, but then the HandleValidationError event handler
is never called for subsequent errors.
Thanks,
<br/>
View the full article