xmldocument and nested schemas
Posted
by
Stuart
on Stack Overflow
See other posts from Stack Overflow
or by Stuart
Published on 2011-01-10T14:38:28Z
Indexed on
2011/01/10
14:53 UTC
Read the original article
Hit count: 215
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.
© Stack Overflow or respective owner