472,127 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Setting default namespace/validating schema w/uncooperative xml?

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
2 2916


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
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.

Similar topics

4 posts views Thread by Lénaïc Huard | last post: by
5 posts views Thread by Ian Malone | last post: by
1 post views Thread by Plop69 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.