Simple code to validate an XML file against a schema file (XSD)
This article is about XML and XSD validation.
Introduction
I had a need to validate an XML file against a schema file (XSD) in VB.NET. So I wrote a simple app that could do this validation. While I was at it I decided it might be nice to validate the schema file all by itself. The basic core functions could be reused in your code to validate XML files against a schema, or just validate the schema file by itself. So I spent an hour or two to put this SimpleXMLValidator together. Most of the code can be found in partial form in the Visual Studio help files.
Background
I started working on a project where I had the option to use a flat file fixed length format or XML file with a schema. Since there was the need to verify that we didn't lose any data on the transfer of the file to our system, I decided on the XML file with schema (not that I would ever choose a flat file over XML). I figured this would also help catch any changes to the XML format. You know how vendors change their format and don't tell you. Then it is a big guessing game to what changed. This little app helps you identify what is wrong, so you know where to look for, to fix the problem.
The code
There are two functions and one sub that do the real work. Each of the functions calls a delegate (which is the one sub) if a validation error occurs. Note that the signature of the sub is important, (that is, the input variables to the procedure must match the delegate signature). All I am really doing in this sub is setting a variable to mark the file as failed validation and displaying the error in a RichTextBox
. Here's the code:
'vb.net
Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As _
ValidationEventArgs)
'Display the validation error. This is only called on error
m_Success = False 'Validation failed
writertbox("Validation error: " + args.Message)
End Sub
//C#
private void ValidationCallBack(Object sender, ValidationEventArgs args)
{
//Display the validation error. This is only called on error
m_Success = false; //Validation failed
writertbox("Validation error: " + args.Message);
}
Next we have the XML validation code. Really the only important prerequisite is that the XML file should point to the correct schema file (XSD) inside the XML file on the XSI tag.
Here is an example:
<?xml version="1.0" encoding="utf-8"?>
<ROOTElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="yourschema.xsd">
Here is the code for the XML validation function:
'vb.net
Private Function validatexml(ByVal infile As String) As Boolean
'First we create the xmltextreader
Dim xmlr As New XmlTextReader(infile)
'We pass the xmltextreader into the xmlvalidatingreader
'This will validate the xml doc with the schema file
'NOTE the xml file it self points to the schema file
Dim xmlvread As New XmlValidatingReader(xmlr)
' Set the validation event handler
AddHandler xmlvread.ValidationEventHandler, _
AddressOf ValidationCallBack
m_Success = True 'make sure to reset the success var
' Read XML data
While (xmlvread.Read)
End While
'Close the reader.
xmlvread.Close()
'The validationeventhandler is the only thing that would
'set m_Success to false
Return m_Success
End Function
//C#
private bool validateXml(String infile)
{
//First we create the xmltextreader
XmlTextReader xmlr = new XmlTextReader(infile);
//We pass the xmltextreader into the xmlvalidatingreader
//This will validate the xml doc with the schema file
//NOTE the xml file it self points to the schema file
XmlValidatingReader xmlvread = new XmlValidatingReader(xmlr);
// Set the validation event handler
xmlvread.ValidationEventHandler +=
new ValidationEventHandler (ValidationCallBack);
m_Success = true; //make sure to reset the success var
// Read XML data
while (xmlvread.Read()){}
//Close the reader.
xmlvread.Close();
//The validationeventhandler is the only thing that would set
//m_Success to false
return m_Success;
}
Finally, we have the code to validate the schema (XSD) file directly. As mentioned before, this function has a callback to the same on validation event as the XML validation function did.
Here is the code:
'vb.net
Private Function validateSchema(ByVal infilename As String) As Boolean
'this function will validate the schema file (xsd)
Dim sr As StreamReader
Dim myschema As XmlSchema
m_Success = True 'make sure to reset the success var
Try
sr = New StreamReader(infilename)
myschema = XmlSchema.Read(sr, AddressOf ValidationCallBack)
'This compile statement is what ususally catches the errors
myschema.Compile(AddressOf ValidationCallBack)
Finally
sr.Close()
End Try
Return m_Success
End Function
//C#
private bool validateSchema(String infilename)
{
//this function will validate the schema file (xsd)
XmlSchema myschema;
m_Success = true; //make sure to reset the success var
StreamReader sr = new StreamReader(infilename);
try
{
myschema = XmlSchema.Read(sr,
new ValidationEventHandler (ValidationCallBack));
//This compile statement is what ususally catches the errors
myschema.Compile(new ValidationEventHandler (ValidationCallBack));
}
finally
{
sr.Close();
}
return m_Success;
}
Conclusion
Well, that is it. Not really a lot to it. I am sure many of you already knew how to do this. I kind of wish you had published an article, it would have saved me some time. Still the learning process is always enjoyable and never ends.