Mark wrote:
I've been trying the .Validate() method on the XmlDocument to validate some
xml against a schema, but one thing I noted was that unless the document
explicitly declares the schema as a default namespace, Validate() just lets
it slide through as a success.
Is there a way to Validate() either a) mandating against a particular
schema, or at the very least b) supplying a default namespace to be used even
when not explicitly declared?
Schema validation looks at the root element and its element name and
namespace to then look for a matching schema and element definition. If
it does not find a matching schema then it does a lax validation and
only issues warnings that elements are found for which there is no
schema element definition e.g.
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.ValidationEventHandler += delegate(object sender,
ValidationEventArgs vargs) {
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};
XmlReader reader = XmlReader.Create(new
StringReader("<god>Kibo</god>"), readerSettings);
while (reader.Read()) {}
reader.Close();
outputs the following warning:
Warning: Could not find schema information for the element 'god'.
That way you are supposed to find out that there are elements for which
there is no schema found (e.g. because you might have a schema for a
certain target namespace but the XML you get has the elements in no
namespace or in a different namespace).
However the problem with the Validate method of an XmlDocument instance
is that with .NET 2.0 there is no way to set the validation flags, it
always uses the default validation flags which unfortunately suppress
warnings. That is a flaw in the Validate API which I currently know no
workaround for besides running the whole document through a reader where
you can set the validation flags.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/