Reputation: 83
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.
Upvotes: 5
Views: 2279
Reputation: 4124
I also have a nested schema case and I don't find any error in validating.My code looks like follwoing.
private string strLogger = null;
public bool ValidateXml(string path2XMLFile, string path2XSDFile)
{
bool isValidFile = false;
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, path2XSDFile);
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
XmlReader reader = XmlReader.Create(path2XMLFile, settings);
while (reader.Read()) ;
if (String.IsNullOrEmpty(strLogger))
{
isValidFile = true;
}
}
catch (Exception ex)
{
LoggingHandler.Log(ex);
}
return isValidFile;
}
private void settings_ValidationEventHandler(object sender, ValidationEventArgs e)
{
strLogger += System.Environment.NewLine + "Validation Error Message = [" + e.Message + "], " + "Validation Error Severity = [" + e.Severity + "], " + System.Environment.NewLine;
}
Upvotes: 1
Reputation: 6295
I think that what you need to do is to merge the schemas:
http://asp.dotnetheaven.com/howto/doc/Xml/MultipleSchemas.aspx
If they're nested, that means that you'll need to start at the bottom of the hierarchy and load them in that order. I'm not 100% sure because the samples I was able to find don't have, strictly speaking, nested structures, but rather complementary structures. Good luck.
Upvotes: 0