When I load a XMLSchema through the following code:
_XmlDocument = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader reader = XmlReader.Create(documentPath, settings);
_XmlDocument.Load(reader);
reader.Close();
XmlSchema schema = _XMLDocument.Schemas.Schemas().OfType<XmlSchema>().FirstOrDefault();
and do the following unit test code:
Assert.IsNotNull(schema);
Assert.AreEqual(this.schemaSourceURI, schema.SourceUri);
XmlSchemaElement queryElement = schema.Elements.Values.OfType<XmlSchemaElement>().Where(e => e.Name.Equals("QUERY")).FirstOrDefault();
Assert.IsNotNull(queryElement);
Assert.IsTrue(queryElement.Constraints.OfType<XmlSchemaKey>().Count() > 0);
Assert.IsTrue(queryElement.Constraints.OfType<XmlSchemaKeyref>().Count() > 0);
everything works fine.
When I load the xsd schema by
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.w3.org/2001/XMLSchema", file);
schemaSet.Compile();
return schemaSet.Schemas().OfType<XmlSchema>().FirstOrDefault();
XmlSchema schema = schemaSet.Schemas().OfType<XmlSchema>().FirstOrDefault();
then both Assert.IsTrue from the unit test code(above) fail. I load the same file both times.
How do I get XmlSchemaSet to load key constraints? Both schemas are from the same file(.SourceUri are both this.schemaSourceURI).
I don't know why but schemaSet.Add(null, file) with null instead of "http://www.w3.org/2001/XMLSchema" fixed it for me.