Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 1st, 2006, 02:25 PM
Mark
Guest
 
Posts: n/a
Default 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

  #2  
Old August 1st, 2006, 03:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Setting default namespace/validating schema w/uncooperative xml?



Mark wrote:

Quote:
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/
  #3  
Old August 1st, 2006, 05:05 PM
Mark
Guest
 
Posts: n/a
Default Re: Setting default namespace/validating schema w/uncooperative xm

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:
Quote:
>
>
Mark wrote:
>
>
Quote:
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/
>
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles