XmlSchemaSet.Add 方法

定义

将给定的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

重载

Add(XmlSchema)

将指定的 XmlSchema 添加到 XmlSchemaSet

Add(XmlSchemaSet)

将给定的 XmlSchemaSet 中的所有 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

Add(String, String)

将位于指定 URL 的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

Add(String, XmlReader)

XmlReader 中包含的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

Add(XmlSchema)

将指定的 XmlSchema 添加到 XmlSchemaSet

public:
 System::Xml::Schema::XmlSchema ^ Add(System::Xml::Schema::XmlSchema ^ schema);
public System.Xml.Schema.XmlSchema? Add (System.Xml.Schema.XmlSchema schema);
public System.Xml.Schema.XmlSchema Add (System.Xml.Schema.XmlSchema schema);
member this.Add : System.Xml.Schema.XmlSchema -> System.Xml.Schema.XmlSchema
Public Function Add (schema As XmlSchema) As XmlSchema

参数

schema
XmlSchema

添加到 XmlSchemaSetXmlSchema 对象。

返回

如果架构有效,则为 XmlSchema 对象。 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。 否则会引发 XmlSchemaException

例外

该架构无效。

作为参数传递的 XmlSchema 对象为 null

注解

XmlSchema如果 对象已存在于 中,XmlSchemaSetAdd 该方法不执行任何操作。

此方法的功能与 方法的功能 Add 相同。

适用于

Add(XmlSchemaSet)

将给定的 XmlSchemaSet 中的所有 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

public:
 void Add(System::Xml::Schema::XmlSchemaSet ^ schemas);
public void Add (System.Xml.Schema.XmlSchemaSet schemas);
member this.Add : System.Xml.Schema.XmlSchemaSet -> unit
Public Sub Add (schemas As XmlSchemaSet)

参数

schemas
XmlSchemaSet

XmlSchemaSet 对象。

例外

XmlSchemaSet 中的架构无效。

作为参数传递的 XmlSchemaSet 对象为 null

示例

下面的代码示例演示如何将Add架构添加到 XmlSchemaSet,然后使用 方法将 添加到XmlSchemaSet新的 XmlSchemaSet

Dim schemaSet1 As XmlSchemaSet = New XmlSchemaSet
schemaSet1.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd")
schemaSet1.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd")
schemaSet1.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd")

Dim schemaSet2 As XmlSchemaSet = New XmlSchemaSet
schemaSet2.Add(schemaSet1)
XmlSchemaSet schemaSet1 = new XmlSchemaSet();
schemaSet1.Add("http://www.contoso.com/retail", "http://www.contoso.com/retail.xsd");
schemaSet1.Add("http://www.contoso.com/books", "http://www.contoso.com/books.xsd");
schemaSet1.Add("http://www.contoso.com/music", "http://www.contoso.com/music.xsd");

XmlSchemaSet schemaSet2 = new XmlSchemaSet();
schemaSet2.Add(schemaSet1);

注解

必须先成功预处理架构,然后才能将 XmlSchemaSet架构添加到 。 预处理执行以下基本任务。

  1. 根据 W3C XML 架构的规则检查架构的结构有效性,但架构未完全验证。

  2. 解析对内部和外部架构组件的引用。 成功检索的任何导入或包含的架构也会添加到 。XmlSchemaSet 导入的架构添加为单独的 XmlSchema 对象,而包含的架构则成为 包括 XmlSchema的一部分。

IsCompiled如果要添加 的 XmlSchemaSet 属性为 true,则要添加 的 中的所有XmlSchemaSet架构都会添加到 。XmlSchemaSet IsCompiled如果要添加的 的 XmlSchemaSet 属性为 false,则添加的每个架构在添加之前都经过预处理。 如果新添加 XmlSchemaSet 的任何架构无法预处理,则不添加任何架构; XmlSchemaException 而是引发 。 因此,以下两个代码示例不等效。

' First example
schemaSet.Add(schemaSet1)

' Second example
Dim schema As XmlSchema

For Each schema in schemaSet.Schemas()

    schemaSet.Add(schema)

Next
// First example
schemaSet.Add(schemaSet1);

// Second example
foreach(XmlSchema schema in schemaSet.Schemas())
{
    schemaSet.Add(schema);
}

前两个代码示例不等效。 在第一个示例中,如果 中 schemaSet1 存在无效架构,并且其 IsCompiled 属性设置为 false,则不会向 schemaSet添加任何架构。 第二个示例中,在遇到无效架构并引发异常之前,可以向 schemaSet 添加多个架构。

适用于

Add(String, String)

将位于指定 URL 的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

public:
 System::Xml::Schema::XmlSchema ^ Add(System::String ^ targetNamespace, System::String ^ schemaUri);
public System.Xml.Schema.XmlSchema? Add (string? targetNamespace, string schemaUri);
public System.Xml.Schema.XmlSchema Add (string targetNamespace, string schemaUri);
member this.Add : string * string -> System.Xml.Schema.XmlSchema
Public Function Add (targetNamespace As String, schemaUri As String) As XmlSchema

参数

targetNamespace
String

架构的 targetNamespace 属性或 null 以使用架构中指定的 targetNamespace

schemaUri
String

指定要加载的架构的 URL。

返回

如果架构有效,则为 XmlSchema 对象。 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。 否则会引发 XmlSchemaException

例外

该架构无效。

作为参数传递的 URL 为 nullEmpty

示例

下面的代码示例将http://www.contoso.com/books.xsd目标命名空间为 的http://www.contoso.com/books架构添加到 。XmlSchemaSet

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

示例使用 books.xsd 文件作为输入。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

注解

必须先成功预处理架构,然后才能将 XmlSchemaSet架构添加到 。 预处理执行以下基本任务。

  1. 根据 W3C XML 架构的规则检查架构的结构有效性,但架构未完全验证。

  2. 解析对内部和外部架构组件的引用。 成功检索的任何导入或包含的架构也会添加到 。XmlSchemaSet 导入的架构添加为单独的 XmlSchema 对象,而包含的架构则成为 包括 XmlSchema的一部分。

下面是使用 Add 方法时要考虑的重要注意事项。

  • 将架构添加到 XmlSchemaSet 具有与 已包含在 中的架构相同的目标命名空间和架构位置 URL 的 XmlSchemaSet 将返回原始架构对象。

  • 当新架构成功添加到 时 XmlSchemaSetIsCompiledXmlSchemaSet 属性设置为 false

  • 调用 方法时 Add ,将解析 XML 架构中遇到的任何包含或导入元素。 未能解析包含和导入元素会导致架构验证警告,如果 ValidationEventHandler 尚未为 XmlSchemaSet 对象指定任何内容,则不会报告这些警告。

  • 如果将目标命名空间与 中 XmlSchemaSet 已存在的架构相同的架构添加到 , XmlSchemaSet则会添加这两个架构。

    注意

    此行为不同于已过时 XmlSchemaCollection 的对象。

  • AddXmlSchemaSet 方法能够使用架构中定义的目标命名空间,而无需在调用 方法时Add将目标命名空间指定为参数。 在 null 方法的 Add 参数中targetNamespace指定会XmlSchemaSet指示 使用架构中定义的目标命名空间,如以下代码示例所示。

Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
schemaSet.Add(Nothing, "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(null, "books.xsd");

foreach(XmlSchema schema in schemaSet.Schemas("http://www.contoso.com/books"))
{
    schema.Write(Console.Out);
}

在上面的代码示例中, null 指定为 targetNamespace 方法的参数 Add 。 因此, targetNamespace 将使用 books.xml 文件中定义的 。 在这种情况下,如果http://www.contoso.com/books已指定为 targetNamespace 参数,Add则调用 方法的结果将相同。

  • W3C XML 架构允许将没有目标命名空间的架构包含在定义了目标命名空间的架构中。 在这种情况下,未定义目标命名空间的架构被强制转换为包含架构的目标命名空间。 包含的架构被视为定义了该目标命名空间。 同样,可以将没有目标命名空间的架构添加到 方法 XmlSchemaSet 指定 Add 的目标命名空间中,如以下示例所示。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="A" type="xs:string" />
</xs:schema>

如果将上述架构添加到 XmlSchemaSet ,目标命名空间http://www.contoso.com/new/targetnamespace (如下面的代码) 所示,则会将其视为架构中声明的目标命名空间。http://www.contoso.com/new/targetnamespace

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

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/new/targetnamespace", "http://www.contoso.com/targetnamespace.xsd");
foreach(XmlSchema schema in schemaSet.Schemas())
{
    Console.WriteLine(schema.TargetNamespace);
}

适用于

Add(String, XmlReader)

XmlReader 中包含的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSet

public:
 System::Xml::Schema::XmlSchema ^ Add(System::String ^ targetNamespace, System::Xml::XmlReader ^ schemaDocument);
public System.Xml.Schema.XmlSchema? Add (string? targetNamespace, System.Xml.XmlReader schemaDocument);
public System.Xml.Schema.XmlSchema Add (string targetNamespace, System.Xml.XmlReader schemaDocument);
member this.Add : string * System.Xml.XmlReader -> System.Xml.Schema.XmlSchema
Public Function Add (targetNamespace As String, schemaDocument As XmlReader) As XmlSchema

参数

targetNamespace
String

架构的 targetNamespace 属性或 null 以使用架构中指定的 targetNamespace

schemaDocument
XmlReader

XmlReader 对象。

返回

如果架构有效,则为 XmlSchema 对象。 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。 否则会引发 XmlSchemaException

例外

该架构无效。

作为参数传递的 XmlReader 对象为 null

示例

下面的代码示例将 包含在 中XmlTextReader且目标命名空间为 的 http://www.contoso.com/books books.xsd 架构添加到 。XmlSchemaSet

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

注解

必须先成功预处理架构,然后才能将 XmlSchemaSet架构添加到 。 预处理执行以下基本任务。

  1. 根据 W3C XML 架构的规则检查架构的结构有效性,但架构未完全验证。

  2. 解析对内部和外部架构组件的引用。 成功检索的任何导入或包含的架构也会添加到 。XmlSchemaSet 导入的架构添加为单独的 XmlSchema 对象,而包含的架构则成为 包括 XmlSchema的一部分。

下面是使用 Add 方法时要考虑的重要注意事项。

  • 包含的架构导入或包含的成功检索的架构XmlReader也会添加到 。XmlSchemaSet

  • XmlReader如果未定位在根元素上,则会引发 ,XmlSchemaException除非当前项是元素。 如果当前项是元素 xs:schema ,则会将架构文档读入 XmlSchemaSet;否则,将引发 , XmlSchemaException 因为架构无效。

  • XmlReader如果 定位在 XML 节点序列上,则只添加序列中的第一个节点。

  • 如果架构是通过 XmlReader.Create 方法调用创建的,则会忽略 属性的值 ProcessInlineSchema ,因为内联架构处理不适用于 W3C XML 架构文档。

  • XmlResolverXmlReader 属性不用于解析对包含和导入元素中的命名空间或架构位置的引用。 XmlResolver而是使用 的 XmlSchemaSet 属性。

  • AddXmlSchemaSet 方法能够使用架构中定义的目标命名空间,而无需在调用 方法时Add将目标命名空间指定为参数。 指定 nullString.EmptyAdd 方法指示 XmlSchemaSet 使用架构中定义的目标命名空间。 有关此行为的示例,请参阅 Add 方法。

此方法的剩余功能与 方法的功能 Add 相同。

适用于