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)

Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs

將指定的 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如果 物件已存在於 中 XmlSchemaSet ,則 Add 方法不會執行任何動作。

這個方法的功能與 Add 方法的功能相同。

適用於

Add(XmlSchemaSet)

Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs

將指定 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

範例

下列程式碼範例說明如何將架構新增至 XmlSchemaSet ,然後使用 方法將 新增 XmlSchemaSet 至新的 XmlSchemaSetAdd

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)

Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs

將指定 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 屬性,如果使用結構描述中指定的 targetNamespace 則為 null

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 的架構,將會傳回原始架構物件。

  • 成功將新的架構新增至 XmlSchemaSet 時,的 IsCompiledXmlSchemaSet 屬性會設定為 false

  • 呼叫 方法時 Add ,會解析 XML 架構中遇到的任何 include 或 import 元素。 無法解析包含和匯入專案會導致架構驗證警告,如果 ValidationEventHandler 尚未指定 XmlSchemaSet 物件,則不會報告這些警告。

  • 如果具有與 中 XmlSchemaSet 已經存在之架構相同的目標命名空間的架構會新增至 XmlSchemaSet ,則會新增這兩個架構。

    注意

    此行為與過時 XmlSchemaCollection 的物件不同。

  • Add的 方法 XmlSchemaSet 能夠使用架構中定義的目標命名空間,而不需要在呼叫 方法時 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 檔案中定義的 。 在此情況下,如果已指定為 targetNamespace 參數,則 http://www.contoso.com/books 呼叫 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)

Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs
Source:
XmlSchemaSet.cs

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 屬性,如果使用結構描述中指定的 targetNamespace 則為 null

schemaDocument
XmlReader

XmlReader 物件。

傳回

XmlSchema 物件 (如果結構描述有效)。 如果結構描述無效,且指定了 ValidationEventHandler,則會傳回 null,並會引發適當的驗證事件。 否則會擲回 XmlSchemaException

例外狀況

此結構描述無效。

做為參數傳遞的 XmlReader 物件為 null

範例

下列程式碼範例會將 內 XmlTextReader 含的 books.xsd 架構新增至 XmlSchemaSet 的目標命名空間 http://www.contoso.com/books

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 屬性。

  • Add的 方法 XmlSchemaSet 能夠使用架構中定義的目標命名空間,而不需要在呼叫 方法時 Add 將目標命名空間指定為參數。 指定 nullString.EmptyAdd 方法會 XmlSchemaSet 指示 使用架構中定義的目標命名空間。 如需此行為的範例,請參閱 Add 方法。

這個方法的其餘功能與 方法的 Add 相同。

適用於