ARTICLE

XML Schema Validator

Posted by Joe Miguel Articles | XML April 16, 2002
The XML Schema Validator checks if a given XML document is well formed and has a valid schema model.
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.

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;
}

Login to add your contents and source code to this article
post comment
     

it,s very usefull thanks.

Posted by ozhan ostovar Dec 25, 2010

thanks for the code

Posted by Srinivasa Rao Duggi Sep 16, 2010
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
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.
Join a Chapter