473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3007


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are...
5
by: Ian Malone | last post by:
Hi, I'm wondering if it's possible for a document to contain a namespace which is a self-reference. e.g. in the following: <?xml version="1.0"?> <!DOCTYPE rdf:RDF > <rdf:RDF...
5
by: Adam Child | last post by:
Hi All, I'm trying to validate an xml document. I'm having trouble setting the default namespace of the xml document. If I hard encode the namespace in the xml file then everything works fine....
1
by: qdm | last post by:
Below is a snippet that I'm sure is quite familiar to most ... <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" Here, I've given the XMLSchema namespace the prefix 'xsd', and with...
5
by: Matt D | last post by:
I have a bunch of constants used in my web services that the client also needs to have. Is there a way to declare public properties that are part of a class or structure as constants or at least...
2
by: Ian | last post by:
I've inherited some xml that doesn't have a namespace and doesn't have a reference to the schema. The schema doesn't have a targetNamespace attribute. I'm trying to build a test tool, in C#, which...
1
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST> xml file 2
12
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
2
by: Robert W. | last post by:
Hi, I have written a schema, specifying attributeFormDefault and elementFormDefault to be "qualified". If I parse an instance document validating against this schema with the DOMParser from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.