By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,289 Members | 3,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,289 IT Pros & Developers. It's quick & easy.

Setting default namespace/validating schema w/uncooperative xml?

P: n/a
Hi...

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?

Thanks
_mark

Aug 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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/
Aug 1 '06 #2

P: n/a
Hi Martin...

Thanks for the detailed response. The work-around I'd thrown together was
to put
if (ValidateSuccess && !dom.DocumentElement.NamespaceURI.Equals
(schema.TargetNamespace))
// report error

Obviously this only works for one schema in the collection and only catches
when the validator has punted because the root didn't play along.

I'll look into using your method; it seems much more robust.

Thanks
-Mark
"Martin Honnen" wrote:
>

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/
Aug 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.