I am trying to validate XML thru XSD for the first time.
tried all possible ways but unable to validate.
Could anyone let what I am missing.
XML File
--------
--------
XSD File
----------
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLStruc.xsd"
xmlns:mstns="http://tempuri.org/XMLStruc.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
----------
C# Validation code
-------
private void button2_Click(object sender, EventArgs e)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://tempuri.org/XMLStruc.xsd", @"c:\shz\xmlcreate\XMLCREATE\XMLCREATE\XMLStruc.xsd");
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
//settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
//settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas = schemas;
settings.ValidationEventHandler += ValidationError;
// create the XmlReader object
XmlReader reader =
XmlReader.Create("Sample.xml", settings);
// parse the file
while (reader.Read()) ; // empty body
if (valid) // check validation result
{
textBox1.Text = "Document is valid";
} // end if
valid = true; // reset variable
reader.Close(); // close reader stream
}
// event handler for validation error
private void ValidationError(object sender,ValidationEventArgs arguments)
{
textBox1.Text = arguments.Message;
valid = false; // validation failed
}
-------
-------
Regards
Shehzad
Replies
Know the answer? Post it — somebody with the same question will find it here.