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.
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.
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:
Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As _
m_Success = False
writertbox("Validation error: " + args.Message)
private void ValidationCallBack(Object sender, ValidationEventArgs args)
m_Success = false;
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:
Here is the code for the XML validation function:
Private Function validatexml(ByVal infile As String) As Boolean
Dim xmlr As New XmlTextReader(infile)
Dim xmlvread As New XmlValidatingReader(xmlr)
AddHandler xmlvread.ValidationEventHandler, _
m_Success = True
private bool validateXml(String infile)
XmlTextReader xmlr = new XmlTextReader(infile);
XmlValidatingReader xmlvread = new XmlValidatingReader(xmlr);
new ValidationEventHandler (ValidationCallBack);
m_Success = true;
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:
Private Function validateSchema(ByVal infilename As String) As Boolean
Dim sr As StreamReader
Dim myschema As XmlSchema
m_Success = True
sr = New StreamReader(infilename)
myschema = XmlSchema.Read(sr, AddressOf ValidationCallBack)
private bool validateSchema(String infilename)
m_Success = true;
StreamReader sr = new StreamReader(infilename);
myschema = XmlSchema.Read(sr,
new ValidationEventHandler (ValidationCallBack));
myschema.Compile(new ValidationEventHandler (ValidationCallBack));
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.