xmldocument and nested schemas
- by Stuart
Using c# and .net 3.5 I'm trying to validate an xml document against a schema that has includes.
The schemas and there includes are as below
Schema1.xsd - include another.xsd
another.xsd - include base.xsd
When i try to add the Schema1.xsd to the XmlDocument i get the following error.
Type 'YesNoType' is not declared or is not a simple type.
I believe i'm getting this error because the base.xsd file is not being included when i load the Schema1.xsd schema.
I'm trying to use the XmlSchemaSet class and I'm setting the XmlResolver uri to the location of the schemas.
NOTE : All schemas live under the same directory E:\Dev\Main\XmlSchemas
Here is the code
string schemaPath = "E:\\Dev\\Main\\XmlSchemas";
XmlDocument xmlDocSchema = new XmlDocument();
XmlSchemaSet s = new XmlSchemaSet();
XmlUrlResolver resolver = new XmlUrlResolver();
Uri baseUri = new Uri(schemaPath);
resolver.ResolveUri(null, schemaPath);
s.XmlResolver = resolver;
s.Add(null, XmlReader.Create(new System.IO.StreamReader(schemaPath + "\\Schema1.xsd"), new XmlReaderSettings { ValidationType = ValidationType.Schema, XmlResolver = resolver }, new Uri(schemaPath).ToString()));
xmlDocSchema.Schemas.Add(s);
ValidationEventHandler valEventHandler = new ValidationEventHandler
(ValidateNinoDobEvent);
try
{
xmlDocSchema.LoadXml(xml);
xmlDocSchema.Validate(valEventHandler);
}
catch (XmlSchemaValidationException xmlValidationError)
{
// need to interogate the Validation Exception, for possible further
// processing.
string message = xmlValidationError.Message;
return false;
}
Can anyone point me in the right direction regarding validating an xmldocument against a schema with nested includes.