简体   繁体   中英

XmlSchemaSet loads schema without key constraints

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).

我不知道为什么但是schemaSet.Add(null,file)与null而不是“ http://www.w3.org/2001/XMLSchema ”为我修复了它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM