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.
![]()
The Validate button validates the schema and generates message "Document is valid" if it finds an schema as a valid schema.
![]()
If an schema is not a valid schema, it generates to the detailed error listed in the list box.
I have used the XmlValidatingReader class. The source code on the Validate button is listed in the following table.
private void validate_Click(object sender, System.EventArgs e)
{
//reset UI information
error = "";
result.Text ="";
resultErrors.Text = "";
isValid = true;
header = "";
try
{
XmlTextReader xml = new XmlTextReader(file.Text);
XmlValidatingReader xsd = new XmlValidatingReader(xml);
//use schemas or DTDs
if (documentTypeSchema.Checked == true)
{
/schemas - YAAAAAAA
xsd.ValidationType = ValidationType.Schema;
}
else if (documentTypeNone.Checked == true)
{
//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;
}
//and validation errors events go to...
xsd.ValidationEventHandler += new ValidationEventHandler(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())
{
}
xsd.Close();
// Check whether the document is valid or invalid.
if (isValid)
header = "Document is valid";
else
header = "Document is invalid";
}
catch(UnauthorizedAccessException a)
{
//dont have access permission
error = a.Message;
}
catch(Exception a)
{
//and other things that could go wrong
error = a.Message;
}
resultErrors.Text = error;
result.Text = header;
}