XML Schema Validator in VB.NET

The XML Schema Validator checks if a given XML document is well formed and has a valid schema model. If it finds the document is not a valid XML schema, it generates the error telling the problem in the schema.

The Browser button lets you browse for an xml document.

XmlSchema-in-vb.net.jpg

The Validate button validates the schema and generates message "Document is valid" if it finds an schema as a valid schema.

XmlSchema1-in-vb.net.gif

If an schema is not a valid schema, it generates to the detailed error listed in the list box.

XmlSchema2-in-vb.net.gif

I have used the XmlValidatingReader class. The source code on the Validate button is listed in the following table.

Private Sub validate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'reset UI information 
[error] = ""
result.Text = ""
resultErrors.Text = ""
isValid = 
True
header = ""
Try
Dim
 xml As New XmlTextReader(file.Text)
Dim xsd As New XmlValidatingReader(xml)
'use schemas or DTDs 
If documentTypeSchema.Checked = True Then
'schemas - YAAAAAAA 
xsd.ValidationType = ValidationType.Schema
Else
If
 documentTypeNone.Checked = True Then
'so you just want to see if your XML is well formed? 
xsd.ValidationType = ValidationType.None
Else
'why do you want to learn this? its old, no one uses it and they are laughing behind your back. Shame on you! 
xsd.ValidationType = ValidationType.DTD
End If
End
 If 'and validation errors events go to... 
AddHandler xsd.ValidationEventHandler, AddressOf MyValidationEventHandler
'wait until the read is over, its occuring in a different thread - kinda like when your walking to get a cup of coffee and your mind is in Hawaii 
While xsd.Read()
End While
xsd.Close()
' Check whether the document is valid or invalid. 
If isValid Then
header = "Document is valid"
Else
header = "Document is invalid"
End If
Catch
 a As UnauthorizedAccessException
'dont have access permission 
[error] = a.Message
Catch a As Exception
'and other things that could go wrong 
[error] = a.Message
End Try
resultErrors.Text = [error]
result.Text = header
End Sub 'validate_Click


Similar Articles