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

要加入 XmlSchemaXmlSchemaSet 物件。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.

備註

如果 XmlSchema 物件已經存在於 XmlSchemaSet中,則 Add 方法不會執行任何操作。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,然後使用 Add 方法將 XmlSchemaSet 新增至新的 XmlSchemaSetThe 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. 任何成功取得的匯入或包含的架構也會新增至 XmlSchemaSetAny 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.

如果要加入之 XmlSchemaSetIsCompiled 屬性是 true,則要新增之 XmlSchemaSet 中的所有架構都會加入至 XmlSchemaSetIf the IsCompiled property of the XmlSchemaSet to add is true, all schemas in the XmlSchemaSet to add are added to the XmlSchemaSet. 如果要加入之 XmlSchemaSetIsCompiled 屬性是 false,則新增的每個架構都會預先處理。If the IsCompiled property of the XmlSchemaSet to add is false, each schema added is preprocessed before being added. 如果新增的 XmlSchemaSet 中有任何架構無法前置處理,則不會加入任何架構;相反地,會擲回 XmlSchemaExceptionIf 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],則不會將任何架構新增至 schemaSetIn 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 目標命名空間的 http://www.contoso.com/books.xsd 架構加入至 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. 任何成功取得的匯入或包含的架構也會新增至 XmlSchemaSetAny 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.

  • 將架構加入至具有相同目標命名空間和架構位置 URL 的 XmlSchemaSet,做為已包含在 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時,XmlSchemaSetIsCompiled 屬性會設定為 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 元素會產生架構驗證警告,如果未指定 XmlSchemaSet 物件的 ValidationEventHandler,就不會報告這些警告。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.

  • 如果已將具有相同目標命名空間的架構加入 XmlSchemaSetXmlSchemaSet 中已存在的架構,則會加入這兩個架構。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.

  • XmlSchemaSetAdd 方法能夠使用在架構中定義的目標命名空間,而不需要在呼叫 Add 方法時,將目標命名空間指定為參數。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. Add 方法的 targetNamespace 參數中指定 null,會指示 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 會指定為 Add 方法的 targetNamespace 參數。In the code example above, null is specified as the targetNamespace parameter to the Add method. 因此,會使用 books.xml 檔案中所定義的 targetNamespaceAs a result, the targetNamespace defined in the books.xml file is used. 在此情況下,如果 http://www.contoso.com/books 已指定為 targetNamespace 參數,則呼叫 Add 方法的結果會相同。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/targetnamespaceIf 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 的書籍. xsd 架構加入至 XmlSchemaSetThe 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. 任何成功取得的匯入或包含的架構也會新增至 XmlSchemaSetAny 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 中的架構所匯入或包含的架構,也會新增至 XmlSchemaSetSuccessfully retrieved schemas imported or included by the schemas contained in the XmlReader are also added to the XmlSchemaSet.

  • 如果 XmlReader 不位於根項目上,除非目前的專案是元素,否則會擲回 XmlSchemaExceptionIf 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.

  • XmlReaderXmlResolver 屬性不會用來解析 include 和 import 元素中的命名空間或架構位置的參考。The XmlResolver property of the XmlReader is not used to resolve references to namespaces or schema locations in include and import elements. 相反地,會使用 XmlSchemaSetXmlResolver 屬性。Instead, the XmlResolver property of the XmlSchemaSet is used.

  • XmlSchemaSetAdd 方法能夠使用在架構中定義的目標命名空間,而不需要在呼叫 Add 方法時,將目標命名空間指定為參數。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. 指定 Add 方法的 nullString.Empty 會指示 XmlSchemaSet 使用架構中所定義的目標命名空間。Specifying 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.

適用於