Validating XML with XSD
public bool VerifySchema(string xmlFilePath, string strXSDPath)
{
bool IsSucessful = true;
try
{
XmlTextReader r = new XmlTextReader("D:\\usr\\PRASANTH\\VCPP\\COMPlus\\06-10-2005T103217726-EMRGDIARY.xml");
XmlValidatingReader v = new XmlValidatingReader(r);
XmlSchemaCollection xsc = null;
XmlTextReader tr ;
tr = new XmlTextReader(strXSDPath);
xsc = new XmlSchemaCollection();
xsc.Add(null, tr);
v.Schemas.Add (xsc);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler);
while (v.Read())
{
}
v.Close();
// Check whether the document is valid or invalid.
if (isValid)
Console.WriteLine("Document is valid");
else
Console.WriteLine("Document is invalid");
}
catch (Exception ex)
{
IsSucessful = false;
throw new Exception (ex.Message);
}
return IsSucessful;
}
///
/// Validation handler for the validation errors in the XML file
///
///
///
public static void MyValidationEventHandler(object sender, ValidationEventArgs args)
{
isValid = false;
Console.WriteLine("Validation event\n" + args.Message);
}