467,118 Members | 994 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

XML schemas .NET 2.0

I have an XML schema and I am trying to validate any XML document sent
against it.

Here's some of the code:
XmlReaderSettings xmlReaderSettings = new
XmlReaderSettings();
xmlReaderSettings.Schemas.Add(@"http://myschema/myschema.xsd",
"myschema.xsd");
xmlReaderSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationEvent);

xmlReaderSettings.ValidationType = ValidationType.Schema;

xmlReaderSettings.ValidationFlags =
xmlReaderSettings.ValidationFlags |

XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader xmlReader = XmlReader.Create(myXmlDocument,
xmlReaderSettings);

while(xmlReader.Read());

Now, my problem is, if I specify an XML document which breaks the
schema with a <Root xmlns='http://myschema/myschema.xsd'> it throws an
XmlSchemaValidationException - which is good.

However, if I, or the client of my service omits the xmlns tag then
there is no error thrown. If I add an ValidationEventHandler I find
that the difference is that when the schema is specified it is an
XmlSeverityType.Error where as if no xmlns is given it is only an
XmlSeverityType.Warning.

Why? Why? Why? Why? Why? The XML document is wrong, it doesn't
validate. I have told it to validate against the schema the idea being
that if my client(s) send it incorrectly it fails. Yet they can now
get around the validation by not declaring the schema in the XML
document. To me this is stupid, stupid, stupid. Surely one of the
main ideas of the schema is that we do not wholly trust the originator
of the XML to be correct so we wish to check?

Basically - is there anyway around this?

Dec 5 '05 #1
  • viewed: 2653
Share:
1 Reply
If the root of the XML document can not be strictly validated, because of
either

1. lack of element declaration of that item
2. lack of type present based on the xsi:type attribute on that element.

then LAX validation is performed as if the type of that element is
xs:anyType. You will get a validation warning in such cases, not errors. You
should filter for Warnings as well as Errors while accepting these XML
documents.

You can look up the XSD spec which specifies this behavior at:
http://www.w3.org/TR/2004/PER-xmlsch...cvc-assess-elt

Thanks,
Zafar

<pe********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I have an XML schema and I am trying to validate any XML document sent
against it.

Here's some of the code:
XmlReaderSettings xmlReaderSettings = new
XmlReaderSettings();
xmlReaderSettings.Schemas.Add(@"http://myschema/myschema.xsd",
"myschema.xsd");
xmlReaderSettings.ValidationEventHandler += new
ValidationEventHandler(ValidationEvent);

xmlReaderSettings.ValidationType = ValidationType.Schema;

xmlReaderSettings.ValidationFlags =
xmlReaderSettings.ValidationFlags |

XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader xmlReader = XmlReader.Create(myXmlDocument,
xmlReaderSettings);

while(xmlReader.Read());

Now, my problem is, if I specify an XML document which breaks the
schema with a <Root xmlns='http://myschema/myschema.xsd'> it throws an
XmlSchemaValidationException - which is good.

However, if I, or the client of my service omits the xmlns tag then
there is no error thrown. If I add an ValidationEventHandler I find
that the difference is that when the schema is specified it is an
XmlSeverityType.Error where as if no xmlns is given it is only an
XmlSeverityType.Warning.

Why? Why? Why? Why? Why? The XML document is wrong, it doesn't
validate. I have told it to validate against the schema the idea being
that if my client(s) send it incorrectly it fails. Yet they can now
get around the validation by not declaring the schema in the XML
document. To me this is stupid, stupid, stupid. Surely one of the
main ideas of the schema is that we do not wholly trust the originator
of the XML to be correct so we wish to check?

Basically - is there anyway around this?

Dec 6 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by btober@seaworthysys.com | last post: by
7 posts views Thread by Roderick A. Anderson | last post: by
4 posts views Thread by anonymous@coolgroups.com | last post: by
2 posts views Thread by John Jenkins | last post: by
6 posts views Thread by Dennis Gearon | last post: by
reply views Thread by Net Virtual Mailing Lists | last post: by
3 posts views Thread by Sami Marzouki | last post: by
reply views Thread by vihrao@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.