用于编译架构的 XmlSchemaSet

描述 XmlSchemaSet 是一个缓存,其中可以存储和验证 XML 架构定义语言(XSD)架构。

XmlSchemaSet 类

XmlSchemaSet 是一个缓存,用于存储和验证 XML 架构定义语言(XSD)架构。

System.Xml 版本 1.0 中,XML 架构作为架构库加载到 XmlSchemaCollection 类中。 在System.Xml版本 2.0 中,类XmlValidatingReaderXmlSchemaCollection已过时,并已分别由Create方法和XmlSchemaSet类替换。

XmlSchemaSet已被引入来解决许多问题,包括标准兼容性、性能以及过时的Microsoft XML-Data 精简(XDR)架构格式。

下面是XmlSchemaCollection类和XmlSchemaSet类之间的比较。

XmlSchemaCollection XmlSchemaSet
支持Microsoft XDR 和 W3C XML 架构。 仅支持 W3C XML 架构。
架构在调用 Add 方法时编译。 调用Add方法时,架构不会被编译。 这在创建架构库期间提供了性能改进。
每个架构生成单个编译版本,该版本可能导致“架构岛”。因此,所有包含和导入仅限定在该架构范围内。 编译的架构生成单个逻辑架构,即一组架构。 添加到该架构集的架构内的任何导入架构将直接添加到架构集中。 这意味着所有类型都可用于所有架构。
集合中只能存在一个特定目标命名空间的架构。 只要没有类型冲突,就可以添加同一目标命名空间的多个架构。

迁移到 XmlSchemaSet

下面的代码示例提供了从过时XmlSchemaCollection类迁移到新XmlSchemaSet类的指南。 该代码示例演示了两个类之间的以下主要差异。

下面是过时 XmlSchemaCollection 的代码示例。

Dim schemaCollection As XmlSchemaCollection = New XmlSchemaCollection()  
schemaCollection.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaCollection.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema in schemaCollection  
  
   Console.WriteLine(schema.TargetNamespace)  
  
Next  
XmlSchemaCollection schemaCollection = new XmlSchemaCollection();  
schemaCollection.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaCollection.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
  
foreach(XmlSchema schema in schemaCollection)  
{  
   Console.WriteLine(schema.TargetNamespace);  
}  

下面是等效 XmlSchemaSet 的代码示例。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Compile()  
  
Dim schema As XmlSchema  
  
For Each schema in schemaSet.Schemas()  
  
   Console.WriteLine(schema.TargetNamespace)  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Compile();  
  
foreach(XmlSchema schema in schemaSet.Schemas())  
{  
   Console.WriteLine(schema.TargetNamespace);  
}  

添加和检索架构

架构是使用 XmlSchemaSetAdd 方法添加到 XmlSchemaSet 中的。 将架构添加到XmlSchemaSet时,它与目标命名空间 URI 相关联。 可以将目标命名空间 URI 指定为方法的参数 Add ,或者如果未指定目标命名空间,则 XmlSchemaSet 使用架构中定义的目标命名空间。

架构是使用 XmlSchemaSetSchemas 属性从 XmlSchemaSet 检索的。 使用Schemas属性,XmlSchemaSet可以迭代遍历XmlSchema中包含的XmlSchemaSet对象。 该 Schemas 属性返回 XmlSchema 包含在 XmlSchemaSet目标命名空间中的所有对象,或者给定目标命名空间参数返回属于目标命名空间的所有 XmlSchema 对象。 如果 null 指定为目标命名空间参数,则 Schemas 属性将返回不带命名空间的所有架构。

以下示例将 books.xsd 命名空间中的 http://www.contoso.com/books 架构添加到一个 XmlSchemaSet,从中检索属于 http://www.contoso.com/books 命名空间 XmlSchemaSet的所有架构,然后将这些架构写入其中 Console

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet  
schemaSet.Add("http://www.contoso.com/books", "books.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema In schemaSet.Schemas("http://www.contoso.com/books")  
  
   schema.Write(Console.Out)  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/books", "books.xsd");  
  
foreach (XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/books"))  
{  
   schema.Write(Console.Out);  
}  

有关从 XmlSchemaSet 对象添加和检索架构的详细信息,请参阅 Add 方法和 Schemas 属性参考文档。

编译架构

XmlSchemaSet 中的架构通过 CompileXmlSchemaSet 方法编译为一个逻辑架构。

注释

与过时 XmlSchemaCollection 的类不同,调用方法时 Add 不会编译架构。

如果Compile方法成功执行,则将XmlSchemaSetIsCompiled属性设置为true

注释

如果在XmlSchemaSet中编辑架构,那么IsCompiled属性将不会受到影响。 不会跟踪 XmlSchemaSet 中各个架构方案的更新。 因此,虽然XmlSchemaSet中的一个架构已被更改,但只要未添加或删除XmlSchemaSet中的任何架构,IsCompiled属性仍然可以true

以下示例将 books.xsd 文件添加到 XmlSchemaSet 该方法,然后调用 Compile 该方法。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/books", "books.xsd")  
schemaSet.Compile()  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/books", "books.xsd");  
schemaSet.Compile();  

有关编译 XmlSchemaSet 中的架构的更多信息,请参见 Compile 方法参考文档。

重新处理架构

XmlSchemaSet中重新处理架构时,将执行所有在调用XmlSchemaSetAdd方法时在架构上进行的预处理步骤。 如果对方法的调用 Reprocess 成功,则会将 IsCompiled 该方法的属性 XmlSchemaSet 设置为 false

XmlSchemaSet 执行编译后,若 XmlSchemaSet 中的架构被修改,应使用 Reprocess 方法。

下面的示例演示了如何使用Reprocess方法重新处理添加到XmlSchemaSet的架构。 在使用Compile方法编译XmlSchemaSet并修改添加到XmlSchemaSet的架构之后,即使XmlSchemaSet中的一个架构被修改,IsCompiled属性也会被设置为trueReprocess方法将执行Add方法的所有预处理,并将IsCompiled属性设置为false

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
Dim schema As XmlSchema = schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Compile()  
  
Dim element As XmlSchemaElement = New XmlSchemaElement()  
schema.Items.Add(element)  
element.Name = "book"  
element.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")  
  
schemaSet.Reprocess(schema)  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
XmlSchema schema = schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Compile();  
  
XmlSchemaElement element = new XmlSchemaElement();  
schema.Items.Add(element);  
element.Name = "book";  
element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");  
  
schemaSet.Reprocess(schema);  

有关 XmlSchemaSet 架构重新处理的详细信息,请参阅 Reprocess 方法参考文档。

检查架构

可以使用XmlSchemaSetContains方法检查架构是否包含在XmlSchemaSet中。 Contains 方法使用目标命名空间或要检查的 XmlSchema 对象。 在任一情况下,如果架构包含在XmlSchemaSet中,Contains方法将返回true;否则,它返回false

有关检查架构的详细信息,请参阅 Contains 方法参考文档。

移除架构

架构使用 XmlSchemaSetRemoveRemoveRecursive 方法从 XmlSchemaSet 中移除。 Remove 方法从 XmlSchemaSet 中删除指定的架构,而 RemoveRecursive 方法则从 XmlSchemaSet 中删除指定的架构及其导入的所有架构。

下面的示例演示了向某个 XmlSchemaSet架构添加多个架构,然后使用 RemoveRecursive 该方法删除其中一个架构及其导入的所有架构。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")  
schemaSet.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd")  
  
Dim schema As XmlSchema  
  
For Each schema In schemaSet.Schemas()  
  
   If schema.TargetNamespace = "http://www.contoso.com/music" Then  
      schemaSet.RemoveRecursive(schema)  
   End If  
  
Next  
XmlSchemaSet schemaSet = new XmlSchemaSet();  
schemaSet.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");  
schemaSet.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");  
schemaSet.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd");  
  
foreach (XmlSchema schema in schemaSet.Schemas())  
{  
   if (schema.TargetNamespace == "http://www.contoso.com/music")  
   {  
      schemaSet.RemoveRecursive(schema);  
   }  
}  

有关从中删除 XmlSchemaSet架构的详细信息,请参阅 RemoveRemoveRecursive 方法参考文档。

架构解析和 xs:import

以下示例描述了 XmlSchemaSet 在给定命名空间的多个架构存在于一个 XmlSchemaSet命名空间中时导入架构的行为。

例如,考虑包含 XmlSchemaSet 命名空间的多个架构的 http://www.contoso.com。 具有以下xs:import指令的架构被添加到XmlSchemaSet中。

<xs:import namespace="http://www.contoso.com" schemaLocation="http://www.contoso.com/schema.xsd" />  

XmlSchemaSet 尝试导入 http://www.contoso.com 命名空间的架构,方法是从 http://www.contoso.com/schema.xsd URL 加载该架构。 只有架构文档中声明的架构声明和类型可以在导入架构中使用,即使 http://www.contoso.com 中存在 XmlSchemaSet 命名空间的其他架构文档。 如果无法在 http://www.contoso.com/schema.xsd URL 处找到 schema.xsd 文件,则不会将 http://www.contoso.com 命名空间的架构导入到导入的架构中。

验证 XML 文档

XML 文档可以在XmlSchemaSet中针对架构进行验证。 若要验证 XML 文档,可以将架构添加到 XmlReaderSettings 对象的 XmlSchemaSetSchemas 属性中,也可以将 XmlSchemaSet 添加到 XmlReaderSettings 对象的 Schemas 属性中。 然后,类XmlReaderSettings的方法XmlReader使用该Create对象来创建对象XmlReader并验证 XML 文档。

有关使用 XmlSchemaSetxmlSchemaSet 验证 XML 文档的详细信息,请参阅 XML 架构 (XSD) 验证

另请参阅