XmlSchemaSet.Add 方法

定义

将给定的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds the given XML Schema definition language (XSD) schema to the XmlSchemaSet.

重载

Add(XmlSchema)

将指定的 XmlSchema 添加到 XmlSchemaSetAdds the given XmlSchema to the XmlSchemaSet.

Add(XmlSchemaSet)

将给定的 XmlSchemaSet 中的所有 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the XmlSchemaSet.

Add(String, String)

将位于指定 URL 的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet.

Add(String, XmlReader)

XmlReader 中包含的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet.

Add(XmlSchema)

将指定的 XmlSchema 添加到 XmlSchemaSetAdds the given XmlSchema to the XmlSchemaSet.

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 对象。The XmlSchema object to add to the XmlSchemaSet.

返回

如果架构有效,则为 XmlSchema 对象。An XmlSchema object if the schema is valid. 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. 否则会引发 XmlSchemaExceptionOtherwise, an XmlSchemaException is thrown.

异常

该架构无效。The schema is not valid.

作为参数传递的 XmlSchema 对象为 nullThe XmlSchema object passed as a parameter is null.

注解

如果中已Add存在XmlSchema对象,则此方法不XmlSchemaSet执行任何操作。If the XmlSchema object already exists in the XmlSchemaSet, the Add method does nothing.

此方法的功能与Add方法的功能相同。The functionality of this method is identical to that of the Add method.

Add(XmlSchemaSet)

将给定的 XmlSchemaSet 中的所有 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the 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 对象。The XmlSchemaSet object.

异常

XmlSchemaSet 中的架构无效。A schema in the XmlSchemaSet is not valid.

作为参数传递的 XmlSchemaSet 对象为 nullThe XmlSchemaSet object passed as a parameter is null.

示例

下面的代码示例演示如何将架构添加XmlSchemaSet到, 然后使用XmlSchemaSet Add方法将添加XmlSchemaSet到新的中。The following code example illustrates adding schemas to an XmlSchemaSet, then adding the XmlSchemaSet to a new XmlSchemaSet using the Add method.

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之前, 必须对其进行成功的预处理。Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. 预处理执行以下基本任务。Preprocessing performs the following basic tasks.

  1. 根据 W3C XML 架构的规则检查架构的结构有效性, 但架构未完全验证。The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  2. 解析对内部和外部架构组件的引用。References to internal and external schema components are resolved. 已成功检索的任何导入或包含的架构也会添加XmlSchemaSet到中。Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. 导入的架构被添加XmlSchema为单独的对象, 并且包含的架构构成包括在内XmlSchema的部分。Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

true XmlSchemaSet XmlSchemaSet如果要添加的的XmlSchemaSet属性为, 则将添加中的所有架构添加到中。 IsCompiledIf the IsCompiled property of the XmlSchemaSet to add is true, all schemas in the XmlSchemaSet to add are added to the XmlSchemaSet. 如果要添加的XmlSchemaSet false属性为, 则添加的每个架构都将进行预处理。 IsCompiledIf the IsCompiled property of the XmlSchemaSet to add is false, each schema added is preprocessed before being added. 如果新添加XmlSchemaSet的任何架构无法进行预处理, 则不会添加任何架构; 相反XmlSchemaException , 将引发。If any of the schemas in the newly added XmlSchemaSet fails to be preprocessed, no schemas are added; instead, an XmlSchemaException is thrown. 因此, 下面两个代码示例是不等效的。As a result, the following two code example are not equivalent.

' 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);  
}  

前面的两个代码示例并不是等效的。The previous two code examples are not equivalent. 在第一个示例中, 如果schemaSet1中存在无效的架构, 并且其IsCompiled属性设置false为, 则不会将schemaSet任何架构添加到中。In the first example, if an invalid schema exists in schemaSet1 and its IsCompiled property is set to false, no schemas are added to schemaSet. 在第二个示例中, 可能会在遇到无效的schemaSet架构之前添加多个架构, 并引发异常。In the second example, a number of schemas can be added to schemaSet before an invalid schema is encountered and an exception is thrown.

Add(String, String)

将位于指定 URL 的 XML 架构定义语言 (XSD) 架构添加到 XmlSchemaSetAdds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet.

public:
 System::Xml::Schema::XmlSchema ^ Add(System::String ^ targetNamespace, System::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 以使用架构中指定的 targetNamespaceThe schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaUri
String

指定要加载的架构的 URL。The URL that specifies the schema to load.

返回

如果架构有效,则为 XmlSchema 对象。An XmlSchema object if the schema is valid. 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. 否则会引发 XmlSchemaExceptionOtherwise, an XmlSchemaException is thrown.

异常

该架构无效。The schema is not valid.

作为参数传递的 URL 为 nullEmptyThe URL passed as a parameter is null or Empty.

示例

下面的代码示例将具有http://www.contoso.com/books.xsd目标命名空间的http://www.contoso.com/books架构添加到XmlSchemaSetThe following code example adds the http://www.contoso.com/books.xsd schema with a target namespace of http://www.contoso.com/books to the 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 文件作为输入。The example uses the books.xsd file as input.

<?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之前, 必须对其进行成功的预处理。Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. 预处理执行以下基本任务。Preprocessing performs the following basic tasks.

  1. 根据 W3C XML 架构的规则检查架构的结构有效性, 但架构未完全验证。The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  2. 解析对内部和外部架构组件的引用。References to internal and external schema components are resolved. 已成功检索的任何导入或包含的架构也会添加XmlSchemaSet到中。Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. 导入的架构被添加XmlSchema为单独的对象, 并且包含的架构构成包括在内XmlSchema的部分。Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

以下是使用Add方法时要考虑的重要注意事项。The following are important notes to consider when using the Add method.

  • 如果将架构添加到XmlSchemaSet具有相同目标命名空间和架构位置的 URL XmlSchemaSet , 则将返回原始架构对象。Adding a schema to the XmlSchemaSet with the same target namespace and schema location URL as a schema already contained within the XmlSchemaSet will return the original schema object.

  • 将新架构成功添加XmlSchemaSet到后IsCompiled , 的XmlSchemaSet属性将设置为falseWhen a new schema is successfully added to an XmlSchemaSet, the IsCompiled property of the XmlSchemaSet is set to false.

  • 调用Add方法时, 将解析 XML 架构中遇到的任何 include 或 import 元素。Any include or import elements encountered in an XML schema are resolved when the Add method is called. 如果无法解析 include 和 import 元素, 则会导致架构验证警告, 如果ValidationEventHandlerXmlSchemaSet为对象指定, 则不会报告这些警告。Failure to resolve include and import elements results in a schema validation warning and if no ValidationEventHandler has been specified for the XmlSchemaSet object, these warning will not be reported.

  • 如果将具有与中XmlSchemaSet已存在的架构相同的目标命名空间的架构添加XmlSchemaSet到, 则这两个架构都将添加。If a schema with the same target namespace as a schema that already exists in the XmlSchemaSet is added to the XmlSchemaSet, both schemas are added.

    备注

    此行为与过时XmlSchemaCollection的对象不同。This behavior differs from the obsolete XmlSchemaCollection object.

  • Add Add方法XmlSchemaSet能够使用架构中定义的目标命名空间, 而不需要在调用方法时将目标命名空间指定为参数。The Add method of the XmlSchemaSet has the ability to use the target namespace defined in a schema, rather than requiring the target namespace be specified as a parameter when the Add method is called. null 方法AddtargetNamespace参数中指定将指示XmlSchemaSet使用架构中定义的目标命名空间, 如以下代码示例中所示。Specifying null in the targetNamespace parameter of the Add method instructs the XmlSchemaSet to use the target namespace defined in the schema, as illustrated in the following code example.

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方法的参数。In the code example above, null is specified as the targetNamespace parameter to the Add method. 因此, 将使用 books.xml targetNamespace文件中定义的。As a result, the targetNamespace defined in the books.xml file is used. 在这种情况下, 如果Add http://www.contoso.com/books已将指定为targetNamespace参数, 则调用方法的结果将相同。In this case, the result of calling the Add method would be identical if http://www.contoso.com/books had been specified as the targetNamespace parameter.

  • W3C XML 架构允许在定义了目标命名空间的架构中包含无目标命名空间的架构。W3C XML Schema allows schemas without a target namespace to be included in schemas with a target namespace defined. 在这种情况下, 未定义目标命名空间的架构被强制转换为包含架构的目标命名空间。In this case, the schema without a target namespace defined is coerced into the target namespace of the including schema. 包含的架构被视为已定义该目标命名空间。The included schema is treated as if it had that target namespace defined. 同样, 没有目标命名空间的架构可以添加到XmlSchemaSet并强制转换为Add方法指定的目标命名空间, 如以下示例中所示。Similarly, schemas without a target namespace can be added to the XmlSchemaSet and coerced into the target namespace specified by the Add method, as illustrated in the following example.
<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声明的目标命名空间为。If the schema above is added to the XmlSchemaSet with the target namespace http://www.contoso.com/new/targetnamespace (as shown in the code below), it is treated as if the target namespace declared in the schema was 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) 架构添加到 XmlSchemaSetAdds the XML Schema definition language (XSD) schema contained in the XmlReader to the 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);
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 以使用架构中指定的 targetNamespaceThe schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaDocument
XmlReader

XmlReader 对象。The XmlReader object.

返回

如果架构有效,则为 XmlSchema 对象。An XmlSchema object if the schema is valid. 如果架构无效且指定了 ValidationEventHandler,则将返回 null 并引发适当的验证事件。If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. 否则会引发 XmlSchemaExceptionOtherwise, an XmlSchemaException is thrown.

异常

该架构无效。The schema is not valid.

作为参数传递的 XmlReader 对象为 nullThe XmlReader object passed as a parameter is null.

示例

下面的代码示例将包含在中的XmlTextReader 、包含的目标http://www.contoso.com/books命名空间的 books.xml 架构添加到XmlSchemaSet中。The following code example adds the books.xsd schema contained in the XmlTextReader with a target namespace of http://www.contoso.com/books to the 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之前, 必须对其进行成功的预处理。Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. 预处理执行以下基本任务。Preprocessing performs the following basic tasks.

  1. 根据 W3C XML 架构的规则检查架构的结构有效性, 但架构未完全验证。The schema is checked for structural validity according to the rules of W3C XML Schema, but the schema is not fully validated.

  2. 解析对内部和外部架构组件的引用。References to internal and external schema components are resolved. 已成功检索的任何导入或包含的架构也会添加XmlSchemaSet到中。Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. 导入的架构被添加XmlSchema为单独的对象, 并且包含的架构构成包括在内XmlSchema的部分。Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

以下是使用Add方法时要考虑的重要注意事项。The following are important notes to consider when using the Add method.

  • 如果成功检索到中包含的架构导入或包含XmlReader的架构, 还会XmlSchemaSet将添加到中。Successfully retrieved schemas imported or included by the schemas contained in the XmlReader are also added to the XmlSchemaSet.

  • 如果未定位在根元素上XmlSchemaException , 则会引发, 除非当前项是一个元素。 XmlReaderIf the XmlReader is not positioned on the root element, an XmlSchemaException is thrown unless the current item is an element. 如果当前项是一个xs:schema元素, 则将架构文档读入XmlSchemaSet; 否则, XmlSchemaException将引发, 因为该架构无效。If the current item is an xs:schema element, the schema document is read into the XmlSchemaSet; otherwise, an XmlSchemaException is thrown because the schema is not valid.

  • XmlReader如果位于 XML 节点序列上, 则只添加序列中的第一个节点。If the XmlReader is positioned over a sequence of XML nodes, only the first node in the sequence is added.

  • 如果架构是通过XmlReader.Create方法调用创建的, 则忽略ProcessInlineSchema属性的值, 因为不会对 W3C XML 架构文档应用内联架构处理。If the schema was created from a XmlReader.Create method call, the value of the ProcessInlineSchema property is ignored, because inline schema processing is not applied for W3C XML Schema documents.

  • 的属性不用于XmlReader解析对 include 和 import 元素中的命名空间或架构位置的引用。 XmlResolverThe XmlResolver property of the XmlReader is not used to resolve references to namespaces or schema locations in include and import elements. 相反, XmlResolver使用的XmlSchemaSet属性。Instead, the XmlResolver property of the XmlSchemaSet is used.

  • Add Add方法XmlSchemaSet能够使用架构中定义的目标命名空间, 而不需要在调用方法时将目标命名空间指定为参数。The Add method of the XmlSchemaSet has the ability to use the target namespace defined in a schema, rather than requiring the target namespace be specified as a parameter when the Add method is called. 如果nullString.Empty或指定Add 为方法,将指示使用架构中定义的目标命名空间。XmlSchemaSetSpecifying null or String.Empty to the Add method instructs the XmlSchemaSet to use the target namespace defined in the schema. 有关此行为的示例, 请参见Add方法。For an example of this behavior, see the Add method.

此方法的其余功能与Add方法的功能相同。The remaining functionality of this method is identical to that of the Add method.

适用于