ARTICLE

XML Schema Validator in VB.NET

Posted by Joe Miguel Articles | Visual Basic .NET November 10, 2012
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.
Reader Level:
Download Files:
 

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

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts