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?