.NET 2.0: XmlDocument and Validation

Could someone give me a basic outline or point me to an article with a sufficient code sample to get me up to speed on how to validate an XmlDocument I've read from file against a schema (.xsd) file that is also on disk? It seems this has been reworked in the DOM for the CLR 2.0 (theXmlValidatingReader class is now obsolete...).
Here is what I worked up based on the scanty help files available with VS 2005 Beta 2:

XmlSchemaSet schemaSet =new XmlSchemaSet();
schemaSet.Add("<nameSpace>", "<path to schema file>");
XmlDocument sessionDocument =new XmlDocument();
sessionDocument.Schemas = schemaSet;
sessionDocument.Load("<path to xml file>");

ValidationEventHandler eventHandler =new ValidationEventHandler(HandleValidationError);
sessionDocument.Validate(eventHandler);


If I alter my XML file so that it violates the schema I'm trying to validate it against here, it does not fire the delegate method
HandleValidationError(). With other objects, etc. it is allowed to call methods similar to Validate (that accept a delegate like this) without the delegate parameter and it will just throw an exception. This method does not allow that, and this stinks as I can think of no good way to have code following the Validate call not execute if the document does not validate.
Any suggestions welcome. Thanks.
[1755 byte] By [NewDawn] at [2008-1-25]
# 1
Here's a complete example that works using anonymous methods in C#:


using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace XmlSchemaVal
{
class Program
{
static void Main(string[] args)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://tempuri.org/XMLSchema1.xsd", "..\\..\\XmlSchema1.xsd");
XmlDocument doc = new XmlDocument();
doc.Schemas = schemas;

bool containsErrors = false;

doc.Load("..\\..\\XmlFile1.xml");
ValidationEventHandler validator = delegate(object sender, ValidationEventArgs e)
{
containsErrors = true;
Console.WriteLine("{0}: {1}", e.Severity, e.Message);
};

doc.Validate(validator);
if (containsErrors)
{
Console.WriteLine("Document Contains Errors");
}
}
}
}

TomasRestrepo at 2007-8-21 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2

Here is the code........the xml file should contain the reference to the xsd file

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Xml.Schema;

/* XmlSchemaCollection necessary when you are using XmlValidatingReader. */

/* XmlValidatingReader is now obselete. */

namespace validateXML

{

class Program

{

static bool valid = true;

static void Main(string[] args)

{

XmlTextReader xmltr = new XmlTextReader("C:\\XML Files\\ebixexport.xml");

XmlReaderSettings xmlrs = new XmlReaderSettings();

xmlrs.ValidationType = ValidationType.Schema;

xmlrs.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;

xmlrs.ValidationEventHandler += new ValidationEventHandler

xmlrs_ValidationEventHandler);

XmlReader xmlrd = XmlReader.Create(xmltr, xmlrs);

while (xmlrd.Read())

{ }

xmlrd.Close();

if (valid)

Console.Write("\n The document is valid.\n");

else

Console.Write("\n The document is not valid.\n");

Console.Read();

}

private static void xmlrs_ValidationEventHandler(object sender, ValidationEventArgs ve)

{

valid = false;

Console.Write(ve.Message);

}

}

}

MihirMehta at 2007-8-21 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3
Thanks! Very useful!
Marcus at 2007-8-21 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified