Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml validation using XSD schema

Tags:

c#

validation

xsd

The following code helps me validate an XML file with an XSD schema.

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
XmlDocument document = new XmlDocument();
document.Load(xmlFilePath);
XmlReader rdr = XmlReader.Create(new StringReader(document.InnerXml), settings);

while (rdr.Read())
{

}
isValid = true;

The ValidationEventHandler also tells me what the errors are, but doesn't tell me on 'where' or 'on which line' they are located. Is there any way to get the line number where the XML fails to be validated?

like image 739
Elroy Avatar asked Feb 21 '09 11:02

Elroy


People also ask

How you will validate XML document using schema?

XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document. The System. Xml.

Can we validate XML documents against a schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

What is XSD validation?

The WSRR web user interface validates each definition file when starting, and when new or updated definition files are loaded. This is done according to the definition XML Schema Definition (XSD).


2 Answers

Would not this do what you are after ?

Create an XmlReaderSettings object and enable warnings through that object.

Unfortunately, there seems to be no way to pass your own XmlReaderSettings object to XmlDocument.Validate().
Instead, you can use a validating XmlReader and an XmlNodeReader to validate an existing XmlDocument (using a XmlNodeReader with StringReader rather than an XmlDocument)

XmlDocument x = new XmlDocument();
x.LoadXml(XmlSource);

XmlReaderSettings settings = new XmlReaderSettings();
settings.CloseInput = true;     
settings.ValidationEventHandler += Handler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, ExtendedTreeViewSchema);
settings.ValidationFlags =
     XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation ;

StringReader r = new StringReader(XmlSource);

using (XmlReader validatingReader = XmlReader.Create(r, settings)) {
        while (validatingReader.Read()) { /* just loop through document */ }
}

And the handler:

private static void Handler(object sender, ValidationEventArgs e)
{
        if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
          System.Diagnostics.Trace.WriteLine(
            String.Format("Line: {0}, Position: {1} \"{2}\"",
                e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message));
}
like image 134
VonC Avatar answered Oct 12 '22 13:10

VonC


ValidationEventArgs.Message includes line/column in its text.

ValidationEventArgs.Exception has fields for line and column.

like image 39
Richard Avatar answered Oct 12 '22 12:10

Richard