|
|
|
start date: Mon, 13 Aug 2007 09:00:33 -0000,
posted on: microsoft.public.dotnet.xml
back
| Thread Index |
|
1
SimonB
|
|
2
Martin Honnen
|
|
3
SimonB
|
XML schema validation
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(ValidationErrorEvent));
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
Date:Mon, 13 Aug 2007 09:00:33 -0000
Author:
|
Re: XML schema validation
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(ValidationErrorEvent));
>
> 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/
Date:Mon, 13 Aug 2007 14:26:10 +0200
Author:
|
Re: XML schema validation
On 13 Aug., 14:26, Martin Honnen wrote:
> 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(ValidationErrorEvent));
>
> > 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
Date:Tue, 21 Aug 2007 08:59:34 -0000
Author:
|
|
|