I wrote a method to validate and xml file against a schema.
If the file does not conform to the schema, it throws an error. It works
fine except for one curious thing.
If I try to validate an XDocument (containing schema xml) against a schema
url, it validates successfully..... Has anyone ever seen this before or know
why it does this.
Here is the code:
public static bool ConformsToSchema ( this XDocument xDocument, string
schemaUrl, out string errorMessage )
{
XmlSchemaSet xmlSchemaSet = new XmlSchemaSet();
bool? documentValidated = null;
try
{
xmlSchemaSet.Add(null, schemaUrl);
xDocument.Validate(xmlSchemaSet, null);
errorMessage = string.Empty;
documentValidated = true;
}
catch (Exception ex)
{
errorMessage = string.Format("Xml validation error occurred.
Document validated unsuccessfully. {0}", ex.Message);
documentValidated = false;
}
return documentValidated ?? false;
}
Many thanks