XML Validator Utility


Hi All

I have created simple but very useful utility, XML Validator. This tool takes  xml file and validates it  against the given XSD .

Enjoy..

Code Snippet:

private void button1_Click(object sender, EventArgs e)
{
Outcome.Text = "Provided XML is valid.";
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add("generic", TheXsd.Text);
Validate(TheXml.Text, xsc);
}

private void Validate(String filename, XmlSchemaCollection xsc)
{
XmlTextReader reader = null;
XmlValidatingReader vreader = null;

reader = new XmlTextReader(filename);
vreader = new XmlValidatingReader(reader);
vreader.Schemas.Add(xsc);
vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
try
{
while (vreader.Read()) { }
}
catch
{
Outcome.Text = “XML Document is not well-formed.”;
}
vreader.Close();
}

public void ValidationCallBack(object sender, ValidationEventArgs args)
{
MessageBox.Show(“Validation error: ” + args.Message);
}

private void Form1_Load(object sender, EventArgs e)
{
Outcome.Text = “”;
}

private void button2_Click(object sender, EventArgs e)
{

openFileDialog1.ShowDialog();
TheXml.Text= openFileDialog1.FileName.ToString();

}

private void button3_Click(object sender, EventArgs e)
{
openFileDialog2.ShowDialog();
TheXsd.Text= openFileDialog2.FileName.ToString();
}

ScreenShot:

XML Validator ScreenShot
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s