CodeSnip: Validating XML Data using the XML Schema Definition
page 2 of 4
by Uday Denduluri
Feedback
Average Rating: 
Views (Total / Last 10 Days): 22052/ 43

Description

The code below explains the process of creating an XML Document from an XML file on the local system.

Listing 1

//Physical path of the XML File
string XmlFilePath = @"C:\NewXMLFile.xml";
//Physical path of the XSD File
string XSDFilePath = @"C:\NewXMLFile.xsd";
 
XmlDocument ObjXmlDocument = new XmlDocument();
try
{
ObjXmlDocument.Load(XmlFilePath);
}
 
catch (XmlException ObjXMLException)
{
MessageBox.Show("Not a Well Formed XML Document"+ ObjXMLException.Message);
}
return ObjXmlDocument;

As shown in Listing 1 an object of type XMLDocument is created and loaded with the XML file specified by the XmlFilePath variable. If it is not a valid XML file, the XML exception is raised. A validation message is shown to the user stating that the XML file given is not a valid XML file. Once we know that the XML is a valid document we need to validate it against the XML schema.

The code below explains loading the XSD file and creating a validation event handler for it.

Listing 2

// Load the schema definition
XmlReaderSettings ObjXMLSettings = new XmlReaderSettings ();
ObjXMLSettings.ValidationType = ValidationType.Schema;
ObjXMLSettings.ValidationEventHandler +=
new System.Xml.Schema.ValidationEventHandler (ObjXMLSettings_ValidationEventHandler);
ObjXMLSettings.Schemas.Add (null, XmlReader.Create (XSDFilePath));

XMLReaderSettings - This class is new in the .NET Framework version 2.0. This class specifies a set of features to support the XmlReader object created by the Create method. As shown in Listing 2, we instantiate the class and set the validation type. We also add a validation event handler. The next listing will describe more about the event handler.

The code below explains the validation event handler that will be fired if the XML document does not match the XSD file.

Listing 3

private void ObjXMLSettings_ValidationEventHandler(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Error)
{
ErrorMessage.Append(args.Message);
}
}

The code shown below is the actual statement that fires the validation part.

Listing 4

XmlNodeReader ObjXmlNodeReader = new XmlNodeReader(ObjXmlDocument);
ObjXMLReader = XmlReader.Create (ObjXmlNodeReader, ObjXMLSettings);
// If there XML file does not match the XSD file 
//then Validation event handler is fired in this statement
while (ObjXMLReader.Read ())
{ 
}
if(!String.IsNullOrEmpty(ErrorMessage.ToString()))
{
 MessageBox.Show(ErrorMessage.ToString());
}

As shown in the Listing 4 the object of XMLReader’s method Read is fired. The error message which is populated by validation errors is shown to the user if there are any.


View Entire Article

User Comments

Title: CodeSnip: Validating XML Data using the XML Schema Definition   
Name: A Non E Mous
Date: 2009-03-30 7:36:50 AM
Comment:
This does not actually work. I have tried it in VS2008, giving a valid XSD file and an XML file that does not match the schema in any respect and did not get any errors.

When I made the XML file invalid, the XMLDocument.Load call did raise an exception as expected. Invalidating the XSD also caused an exception in Schemas.Add; however, the functionality claimed in the article title is non-existent.
Title: CodeSnip: Validating XML Data using the XML Schema Definition   
Name: my name? Your name? The name of the article?
Date: 2007-03-19 9:30:20 PM
Comment:
This code won't actually compile. That may be my fault, but I am pretty sure that there are some sytaxish errors here:
if(!String.IsNullOrEmpty(ErrorMessage.ToString()))
{
MessageBox.Show(ErrorMessage.ToString());
}
where does ErrorMessage get defined?






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 11:43:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search