1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public static bool ValidateXml(string strXmlFilePath)
{
XmlReaderSettings settings = null;
XmlReader reader = null;
try
{
// Set the validation settings.
settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
reader = XmlReader.Create(strXmlFilePath, settings);
// Parse the file.
while (reader.Read()) ;
}
catch (Exception E)
{
strErrorDescription += E.Message;
}
finally
{
if (reader != null)
reader.Close();
}
return (StrLastErrorDescription.Length == 0);
}
// affiche toutes les erreurs
private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
strErrorDescription += "Warning: Matching schema not found. No validation occurred." + args.Message+"\n";
else
strErrorDescription += "Validation error: " + args.Message+"\n";
} |