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:
Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As _
ValidationEventArgs)
m_Success = False
writertbox("Validation error: " + args.Message)
End Sub
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:
="1.0"="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:
Private Function validatexml(ByVal infile As String) As Boolean
Dim xmlr As New XmlTextReader(infile)
Dim xmlvread As New XmlValidatingReader(xmlr)
AddHandler xmlvread.ValidationEventHandler, _
AddressOf ValidationCallBack
m_Success = True
While (xmlvread.Read)
End While
xmlvread.Close()
Return m_Success
End Function
private bool validateXml(String infile)
{
XmlTextReader xmlr = new XmlTextReader(infile);
XmlValidatingReader xmlvread = new XmlValidatingReader(xmlr);
xmlvread.ValidationEventHandler +=
new ValidationEventHandler (ValidationCallBack);
m_Success = true;
while (xmlvread.Read()){}
xmlvread.Close();
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:
Private Function validateSchema(ByVal infilename As String) As Boolean
Dim sr As StreamReader
Dim myschema As XmlSchema
m_Success = True
Try
sr = New StreamReader(infilename)
myschema = XmlSchema.Read(sr, AddressOf ValidationCallBack)
myschema.Compile(AddressOf ValidationCallBack)
Finally
sr.Close()
End Try
Return m_Success
End Function
private bool validateSchema(String infilename)
{
XmlSchema myschema;
m_Success = true;
StreamReader sr = new StreamReader(infilename);
try
{
myschema = XmlSchema.Read(sr,
new ValidationEventHandler (ValidationCallBack));
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.
I started my programmer career over 26 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with vb.net when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out asp.net in 2004. I achieved my MCSD.net in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between vb.net and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2019.
On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.