![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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. |
#3
| |||
| |||
|
|
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. |
#4
| |||
| |||
|
|
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/ |
#5
| |||
| |||
|
|
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/ |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |