HighTechTalks DotNet Forums  

XML schema validation

Dotnet XML microsoft.public.dotnet.xml


Discuss XML schema validation in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
SimonB
 
Posts: n/a

Default XML schema validation - 08-13-2007 , 05:00 AM






Hi,

I have a XML file which I validate against a schema using the
following:

doc.Schemas.Add("", xsdSchema);
doc.Validate(new
System.Xml.Schema.ValidationEventHandler(Validatio nErrorEvent));

This works fine, and I get the correct error when changing element
names and structure.
But when the XML to validate contains a different namespace than the
one trying to validate I don't get an error, I can see why. So my
question is how do I define the schema or my code to fail validation
when a wrong NS is contained in the XML.

Samples:
<root><elements>value</elements></root> // fails when containing a
structureerror.

<root xmlns="http://random-ns"><elements>value</elements></root> //
doesn't fail.

Thanks
Simon


Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XML schema validation - 08-13-2007 , 08:26 AM






SimonB wrote:

Quote:
I have a XML file which I validate against a schema using the
following:

doc.Schemas.Add("", xsdSchema);
doc.Validate(new
System.Xml.Schema.ValidationEventHandler(Validatio nErrorEvent));

This works fine, and I get the correct error when changing element
names and structure.
But when the XML to validate contains a different namespace than the
one trying to validate I don't get an error, I can see why. So my
question is how do I define the schema or my code to fail validation
when a wrong NS is contained in the XML.
In terms of the XML schema language it is not a validation error when no
matching schema is found for the root element namespace.
The .NET 2.0 validating parser can give you a warning if you set a
certain flag but that is not possible with the method Validate you are
using. In that case all you can do is check whether e.g.
doc.DocumentElement.NamespaceURI
is what you are looking for (e.g.
doc.DocumentElement.NamespaceURI == ""
with C# to check whether to root element is in no namespace).

If you are not using the Validate method but rather validate while
parsing the XML with XmlReader then you can use
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
and you will get a warning that no matching schema for the root element
and its namespace has been found.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XML schema validation - 08-13-2007 , 08:26 AM



SimonB wrote:

Quote:
I have a XML file which I validate against a schema using the
following:

doc.Schemas.Add("", xsdSchema);
doc.Validate(new
System.Xml.Schema.ValidationEventHandler(Validatio nErrorEvent));

This works fine, and I get the correct error when changing element
names and structure.
But when the XML to validate contains a different namespace than the
one trying to validate I don't get an error, I can see why. So my
question is how do I define the schema or my code to fail validation
when a wrong NS is contained in the XML.
In terms of the XML schema language it is not a validation error when no
matching schema is found for the root element namespace.
The .NET 2.0 validating parser can give you a warning if you set a
certain flag but that is not possible with the method Validate you are
using. In that case all you can do is check whether e.g.
doc.DocumentElement.NamespaceURI
is what you are looking for (e.g.
doc.DocumentElement.NamespaceURI == ""
with C# to check whether to root element is in no namespace).

If you are not using the Validate method but rather validate while
parsing the XML with XmlReader then you can use
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
and you will get a warning that no matching schema for the root element
and its namespace has been found.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
SimonB
 
Posts: n/a

Default Re: XML schema validation - 08-21-2007 , 04:59 AM



On 13 Aug., 14:26, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
SimonB wrote:
I have a XML file which I validate against a schema using the
following:

doc.Schemas.Add("", xsdSchema);
doc.Validate(new
System.Xml.Schema.ValidationEventHandler(Validatio nErrorEvent));

This works fine, and I get the correct error when changing element
names and structure.
But when the XML to validate contains a different namespace than the
one trying to validate I don't get an error, I can see why. So my
question is how do I define the schema or my code to fail validation
when a wrong NS is contained in the XML.

In terms of the XML schema language it is not a validation error when no
matching schema is found for the root element namespace.
The .NET 2.0 validating parser can give you a warning if you set a
certain flag but that is not possible with the method Validate you are
using. In that case all you can do is check whether e.g.
doc.DocumentElement.NamespaceURI
is what you are looking for (e.g.
doc.DocumentElement.NamespaceURI == ""
with C# to check whether to root element is in no namespace).

If you are not using the Validate method but rather validate while
parsing the XML with XmlReader then you can use
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
and you will get a warning that no matching schema for the root element
and its namespace has been found.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Just what I was looking for.
Thanks

/Simon



Reply With Quote
  #5  
Old   
SimonB
 
Posts: n/a

Default Re: XML schema validation - 08-21-2007 , 04:59 AM



On 13 Aug., 14:26, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
SimonB wrote:
I have a XML file which I validate against a schema using the
following:

doc.Schemas.Add("", xsdSchema);
doc.Validate(new
System.Xml.Schema.ValidationEventHandler(Validatio nErrorEvent));

This works fine, and I get the correct error when changing element
names and structure.
But when the XML to validate contains a different namespace than the
one trying to validate I don't get an error, I can see why. So my
question is how do I define the schema or my code to fail validation
when a wrong NS is contained in the XML.

In terms of the XML schema language it is not a validation error when no
matching schema is found for the root element namespace.
The .NET 2.0 validating parser can give you a warning if you set a
certain flag but that is not possible with the method Validate you are
using. In that case all you can do is check whether e.g.
doc.DocumentElement.NamespaceURI
is what you are looking for (e.g.
doc.DocumentElement.NamespaceURI == ""
with C# to check whether to root element is in no namespace).

If you are not using the Validate method but rather validate while
parsing the XML with XmlReader then you can use
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationFlags |=
XmlSchemaValidationFlags.ReportValidationWarnings;
and you will get a warning that no matching schema for the root element
and its namespace has been found.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Just what I was looking for.
Thanks

/Simon



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.