संपादित करें

इसके माध्यम से साझा किया गया


XmlSchemaSet.Add Method

Definition

Adds the given XML Schema definition language (XSD) schema to the XmlSchemaSet.

Overloads

Add(XmlSchema)

Adds the given XmlSchema to the XmlSchemaSet.

Add(XmlSchemaSet)

Adds all the XML Schema definition language (XSD) schemas in the given XmlSchemaSet to the XmlSchemaSet.

Add(String, String)

Adds the XML Schema definition language (XSD) schema at the URL specified to the XmlSchemaSet.

Add(String, XmlReader)

Adds the XML Schema definition language (XSD) schema contained in the XmlReader to the XmlSchemaSet.

Add(XmlSchema)

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

Adds 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);
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

Parameters

schema
XmlSchema

The XmlSchema object to add to the XmlSchemaSet.

Returns

An XmlSchema object if the schema is valid. If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. Otherwise, an XmlSchemaException is thrown.

Exceptions

The schema is not valid.

The XmlSchema object passed as a parameter is null.

Remarks

If the XmlSchema object already exists in the XmlSchemaSet, the Add method does nothing.

The functionality of this method is identical to that of the Add method.

Applies to

Add(XmlSchemaSet)

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

Adds 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)

Parameters

schemas
XmlSchemaSet

The XmlSchemaSet object.

Exceptions

A schema in the XmlSchemaSet is not valid.

The XmlSchemaSet object passed as a parameter is null.

Examples

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

Remarks

Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  1. 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. Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

If the IsCompiled property of the XmlSchemaSet to add is true, all schemas in the XmlSchemaSet to add are added to the XmlSchemaSet. If the IsCompiled property of the XmlSchemaSet to add is false, each schema added is preprocessed before being added. 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. 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. In the second example, a number of schemas can be added to schemaSet before an invalid schema is encountered and an exception is thrown.

Applies to

Add(String, String)

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

Adds 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);
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

Parameters

targetNamespace
String

The schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaUri
String

The URL that specifies the schema to load.

Returns

An XmlSchema object if the schema is valid. If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. Otherwise, an XmlSchemaException is thrown.

Exceptions

The schema is not valid.

The URL passed as a parameter is null or Empty.

Examples

The 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");

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>

Remarks

Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  1. 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. Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

The following are important notes to consider when using the Add method.

  • 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.

  • When a new schema is successfully added to an XmlSchemaSet, the IsCompiled property of the XmlSchemaSet is set to false.

  • Any include or import elements encountered in an XML schema are resolved when the Add method is called. 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.

  • 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.

    Note

    This behavior differs from the obsolete XmlSchemaCollection object.

  • 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. 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);
}

In the code example above, null is specified as the targetNamespace parameter to the Add method. As a result, the targetNamespace defined in the books.xml file is used. 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 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. 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>

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

Applies to

Add(String, XmlReader)

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

Adds 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);
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

Parameters

targetNamespace
String

The schema targetNamespace property, or null to use the targetNamespace specified in the schema.

schemaDocument
XmlReader

The XmlReader object.

Returns

An XmlSchema object if the schema is valid. If the schema is not valid and a ValidationEventHandler is specified, then null is returned and the appropriate validation event is raised. Otherwise, an XmlSchemaException is thrown.

Exceptions

The schema is not valid.

The XmlReader object passed as a parameter is null.

Examples

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");

Remarks

Before a schema can be added to an XmlSchemaSet, it has to be successfully preprocessed. Preprocessing performs the following basic tasks.

  1. 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. Any imported or included schemas that are successfully retrieved are also added to the XmlSchemaSet. Imported schemas are added as separate XmlSchema objects, and included schemas are made a part of the including XmlSchema.

The following are important notes to consider when using the Add method.

  • Successfully retrieved schemas imported or included by the schemas contained in the XmlReader are also added to the XmlSchemaSet.

  • If the XmlReader is not positioned on the root element, an XmlSchemaException is thrown unless the current item is an element. 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.

  • If the XmlReader is positioned over a sequence of XML nodes, only the first node in the sequence is added.

  • 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.

  • The XmlResolver property of the XmlReader is not used to resolve references to namespaces or schema locations in include and import elements. Instead, the XmlResolver property of the XmlSchemaSet is used.

  • 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. Specifying null or String.Empty to the Add method instructs the XmlSchemaSet to use the target namespace defined in the schema. For an example of this behavior, see the Add method.

The remaining functionality of this method is identical to that of the Add method.

Applies to