简体   繁体   English

XmlSchemaSet在没有键约束的情况下加载模式

[英]XmlSchemaSet loads schema without key constraints

When I load a XMLSchema through the following code: 当我通过以下代码加载XMLSchema时:

_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 当我加载xsd架构时

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. 然后来自单元测试代码(上面)的Assert.IsTrue都失败了。 I load the same file both times. 我两次加载相同的文件。

How do I get XmlSchemaSet to load key constraints? 如何让XmlSchemaSet加载键约束? Both schemas are from the same file(.SourceUri are both this.schemaSourceURI). 两个模式都来自同一个文件(.SourceUri都是this.schemaSourceURI)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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