Alex Mueller on Software and Technology 
Friday, August 19, 2005

In my first project in Whidbey where I have a customer and a deadline, I ran into some new features where I could not find any examples on the internet. Specifically, I am using XPathDocument and XPathNavigator. After implementing my code, I then proceeded to fill out my test cases to verify my expected results. In one scenario, I need to verify the XML being returned by my code is validated against the schema I customized for it. As it turns out, the validation revealed that my XML needed to be tweaked, before I could commit my code.

A simple way to validate XML against a schema is to use XPathNavigator.CheckValidity(), which is new for 2.0. If the XML is valid, the function returns true, otherwise false, by means of an exception. At the time I wrote this, I actually could find nothing via Google to show me how to implement this, other than the Visual Studio 2005 Documentation, which is pre-release documentation and subject to change. As a result, I have decided to show my code here, while quite simple, to give an example for those looking for one.

In order to check the validity of our XML in our XPathDocument against a schema, we need to set up the appropriate objects to handle this.

CheckValidity requires an XmlSchemaSet and a ValidationEventHandler.
public virtual bool CheckValidity(XmlSchemaSet schemas, ValidationEventHandler validationEventHandler);

XmlSchemaSet is also new for 2.0, and has been introduced to fix a number of issues, including standards compatibility, performance, and the obsolete Microsoft XML-Data Reduced (XDR) schema format. In my example, I only want to validate my XML against one schema. I must use the XmlSchema class in order to load my customized schema. I then add my schema to my XmlSchemaSet to use for validation. Of course there are other ways to validate XML against an XSD, so do what you wish.

The second part of CheckValidity is the ValidationEventHandler that receives information about schema validation warnings and errors. I recommend not setting this null, and processing the errors should they arise.

Assuming we already have an XPathDocument and an XPathNavigator created, we have the following.

// Create an event handler to trap Validation errors.
ValidationEventHandler validationHandler = new ValidationEventHandler(XmlValidationError);

// Create XmlSchemaSet and XmlSchema
XmlSchemaSet set = new XmlSchemaSet();
XmlSchema schema = XmlSchema.Read(new XmlTextReader(@"C:\Temp\MySchema.xsd"), validationHandler);

// Add our schema to the XmlSchemaSet
set.Add(schema);

// Now we can validate
if (!xpathNavigator.CheckValidity(set, validationHandler))
    // invalid - do something
else
    // valid - do something
}

// Define our event handler outside the scope of this code
// Handles XML Validation errors.
static void XmlValidationError(object sender, ValidationEventArgs ex)
{
    throw new XmlSchemaValidationException(ex.Message);
}

One note, we really do not need to create a new instance of our ValidationEventHandler. We could pass null into our XmlSchema.Read() and create a new reference to our event handler in our CheckValidity() function.

Again, there are a number of ways to validate your XML against an XSD. In my case, I was working with the XPathDocument and XPathNavigator objects. I decided to take advantage of the XPathNavigator.CheckValidity() method new in the 2.0 framework.

Friday, August 19, 2005 2:38:56 PM (Mountain Standard Time, UTC-07:00) | Comments [1] | #
MuellerDesigns.net
Search
On This Page
SketchPath XPath Editor
Software Testing - Revisited
Architecting Buildings and Software
NBCOlympics.com with Silverlight
Marker Interfaces and C# Attributes
The Phone Screen
Working with ASP.NET MVC and MvcContrib
Thanks to BDD
Twitter
The Opposite of a Singleton?
Removing Duplicate Code in Functions
Add Vista Themes to Longhorn
Changing File Ownership In Vista and Longhorn
ReSharper Type Completion
Caring for the Team
Most Popular
JavaScript ReplaceAll Functionality
What is polymorphism?
What is composition?
Sorting with IComparable and IComparer
Applying the Observer Pattern in ASP.NET
MVP in ASP.NET
What is abstraction?
What is encapsulation?
What is a class?
What is inheritance?
Authentication in ASP.NET
Calendar Controls
SQL Server 2005 Connection Issues
XPathNavigator.CheckValidity new for 2.0
Auto-attach to process '[####] aspnet_wp.exe' on m...
What is an object?
FreeTextBox
VMWare and VPC
An Example of Reflection using C#
A New NHibernate Blog
Archive
Links
Categories
My Local Blog Map
Blogroll
About
Powered by:

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
MuellerDesigns.net

Sign In

Help Those In Need
The Hunger Site
Ronald McDonald House Charities (RMHC) of Western Washington & Alaska