description | title | ms.date | dev_langs | ms.assetid | |||
---|---|---|---|---|---|---|---|
Learn more about: Editing XML Schemas |
Editing XML Schemas |
03/30/2017 |
|
fa09c8e5-c2b9-49d2-bb0d-40330cd13e4d |
Editing an XML schema is one of the most important features of the Schema Object Model (SOM). All of the pre-schema-compilation properties of the SOM can be used to change the existing values in an XML schema. The XML schema can then be recompiled to reflect the changes.
The first step in editing a schema loaded into the SOM is to traverse the schema. You should be familiar with traversing a schema using the SOM API before you attempt to edit a schema. You should also be familiar with the pre- and post-schema-compilation properties of the Post-Schema-Compilation-Infoset (PSCI).
In this section, two code examples are provided, both of which edit the customer schema created in the Building XML Schemas topic. The first code example adds a new PhoneNumber
element to the Customer
element and the second code example adds a new Title
attribute to the FirstName
element. The first sample also uses the post-schema-compilation xref:System.Xml.Schema.XmlSchema.Elements%2A?displayProperty=nameWithType collection as the means of traversing the customer schema while the second code example uses the pre-schema-compilation xref:System.Xml.Schema.XmlSchema.Items%2A?displayProperty=nameWithType collection.
This first code example adds a new PhoneNumber
element to the Customer
element of the customer schema. The code example edits the customer schema in the following steps.
-
Adds the customer schema to a new xref:System.Xml.Schema.XmlSchemaSet object and then compiles it. Any schema validation warnings and errors encountered reading or compiling the schema are handled by the xref:System.Xml.Schema.ValidationEventHandler delegate.
-
Retrieves the compiled xref:System.Xml.Schema.XmlSchema object from the xref:System.Xml.Schema.XmlSchemaSet by iterating over the xref:System.Xml.Schema.XmlSchemaSet.Schemas%2A property. Because the schema is compiled, Post-Schema-Compilation-Infoset (PSCI) properties are accessible.
-
Creates the
PhoneNumber
element using the xref:System.Xml.Schema.XmlSchemaElement class, thexs:string
simple type restriction using the xref:System.Xml.Schema.XmlSchemaSimpleType and xref:System.Xml.Schema.XmlSchemaSimpleTypeRestriction classes, adds a pattern facet to the xref:System.Xml.Schema.XmlSchemaSimpleTypeRestriction.Facets%2A property of the restriction, and adds the restriction to the xref:System.Xml.Schema.XmlSchemaSimpleType.Content%2A property of the simple type and the simple type to the xref:System.Xml.Schema.XmlSchemaElement.SchemaType%2A of thePhoneNumber
element. -
Iterates over each xref:System.Xml.Schema.XmlSchemaElement in the xref:System.Xml.Schema.XmlSchemaObjectTable.Values%2A collection of the post-schema-compilation xref:System.Xml.Schema.XmlSchema.Elements%2A?displayProperty=nameWithType collection.
-
If the xref:System.Xml.Schema.XmlSchemaElement.QualifiedName%2A of the element is
"Customer"
, gets the complex type of theCustomer
element using the xref:System.Xml.Schema.XmlSchemaComplexType class and the sequence particle of the complex type using the xref:System.Xml.Schema.XmlSchemaSequence class. -
Adds the new
PhoneNumber
element to the sequence containing the existingFirstName
andLastName
elements using the pre-schema-compilation xref:System.Xml.Schema.XmlSchemaSequence.Items%2A collection of the sequence. -
Finally, reprocesses and compiles the modified xref:System.Xml.Schema.XmlSchema object using the xref:System.Xml.Schema.XmlSchemaSet.Reprocess%2A and xref:System.Xml.Schema.XmlSchemaSet.Compile%2A methods of the xref:System.Xml.Schema.XmlSchemaSet class and writes it to the console.
The following is the complete code example.
[!code-cppXmlSchemaEditExample1#1] [!code-csharpXmlSchemaEditExample1#1] [!code-vbXmlSchemaEditExample1#1]
The following is the modified customer schema created in the Building XML Schemas topic.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://www.tempuri.org" targetNamespace="http://www.tempuri.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="tns:LastNameType" />
<xs:element name="PhoneNumber"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="\d{3}-\d{3}-\d(4)" /> </xs:restriction> </xs:simpleType> </xs:element>
</xs:sequence>
<xs:attribute name="CustomerId" type="xs:positiveInteger" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="LastNameType">
<xs:restriction base="xs:string">
<xs:maxLength value="20" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
This second code example, adds a new Title
attribute to the FirstName
element of the customer schema. In the first code example, the type of the FirstName
element is xs:string
. For the FirstName
element to have an attribute along with string content, its type must be changed to a complex type with a simple content extension content model.
The code example edits the customer schema in the following steps.
-
Adds the customer schema to a new xref:System.Xml.Schema.XmlSchemaSet object and then compiles it. Any schema validation warnings and errors encountered reading or compiling the schema are handled by the xref:System.Xml.Schema.ValidationEventHandler delegate.
-
Retrieves the compiled xref:System.Xml.Schema.XmlSchema object from the xref:System.Xml.Schema.XmlSchemaSet by iterating over the xref:System.Xml.Schema.XmlSchemaSet.Schemas%2A property. Because the schema is compiled, Post-Schema-Compilation-Infoset (PSCI) properties are accessible.
-
Creates a new complex type for the
FirstName
element using the xref:System.Xml.Schema.XmlSchemaComplexType class. -
Creates a new simple content extension, with a base type of
xs:string
, using the xref:System.Xml.Schema.XmlSchemaSimpleContent and xref:System.Xml.Schema.XmlSchemaSimpleContentExtension classes. -
Creates the new
Title
attribute using the xref:System.Xml.Schema.XmlSchemaAttribute class, with a xref:System.Xml.Schema.XmlSchemaAttribute.SchemaTypeName%2A ofxs:string
and adds the attribute to the simple content extension. -
Sets the content model of the simple content to the simple content extension and the content model of the complex type to the simple content.
-
Adds the new complex type to the pre-schema-compilation xref:System.Xml.Schema.XmlSchema.Items%2A?displayProperty=nameWithType collection.
-
Iterates over each xref:System.Xml.Schema.XmlSchemaObject in the pre-schema-compilation xref:System.Xml.Schema.XmlSchema.Items%2A?displayProperty=nameWithType collection.
Note
Because the FirstName
element is not a global element in the schema, it is not available in the xref:System.Xml.Schema.XmlSchema.Items%2A?displayProperty=nameWithType or xref:System.Xml.Schema.XmlSchema.Elements%2A?displayProperty=nameWithType collections. The code example locates the FirstName
element by first locating the Customer
element.
The first code example traversed the schema using the post-schema-compilation xref:System.Xml.Schema.XmlSchema.Elements%2A?displayProperty=nameWithType collection. In this sample, the pre-schema-compilation xref:System.Xml.Schema.XmlSchema.Items%2A?displayProperty=nameWithType collection is used to traverse the schema. While both collections provide access to the global elements in the schema, iterating through the xref:System.Xml.Schema.XmlSchema.Items%2A collection is more time consuming because you must iterate over all global elements in the schema and it does not have any PSCI properties. The PSCI collections (xref:System.Xml.Schema.XmlSchema.Elements%2A?displayProperty=nameWithType, xref:System.Xml.Schema.XmlSchema.Attributes%2A?displayProperty=nameWithType, xref:System.Xml.Schema.XmlSchema.SchemaTypes%2A?displayProperty=nameWithType, and so on) provide direct access to their global elements, attributes, and types and their PSCI properties.
-
If the xref:System.Xml.Schema.XmlSchemaObject is an element, whose xref:System.Xml.Schema.XmlSchemaElement.QualifiedName%2A is
"Customer"
, gets the complex type of theCustomer
element using the xref:System.Xml.Schema.XmlSchemaComplexType class and the sequence particle of the complex type using the xref:System.Xml.Schema.XmlSchemaSequence class. -
Iterates over each xref:System.Xml.Schema.XmlSchemaParticle in the pre-schema-compilation xref:System.Xml.Schema.XmlSchemaSequence.Items%2A?displayProperty=nameWithType collection.
-
If the xref:System.Xml.Schema.XmlSchemaParticle is an element, who's xref:System.Xml.Schema.XmlSchemaElement.QualifiedName%2A is
"FirstName"
, sets the xref:System.Xml.Schema.XmlSchemaElement.SchemaTypeName%2A of theFirstName
element to the newFirstName
complex type. -
Finally, reprocesses and compiles the modified xref:System.Xml.Schema.XmlSchema object using the xref:System.Xml.Schema.XmlSchemaSet.Reprocess%2A and xref:System.Xml.Schema.XmlSchemaSet.Compile%2A methods of the xref:System.Xml.Schema.XmlSchemaSet class and writes it to the console.
The following is the complete code example.
[!code-cppXmlSchemaEditExample2#1] [!code-csharpXmlSchemaEditExample2#1] [!code-vbXmlSchemaEditExample2#1]
The following is the modified customer schema created in the Building XML Schemas topic.
<?xml version="1.0" encoding=" utf-8"?>
<xs:schema xmlns:tns="http://www.tempuri.org" targetNamespace="http://www.tempuri.org" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="tns:FirstNameComplexType" />
<xs:element name="LastName" type="tns:LastNameType" />
</xs:sequence>
<xs:attribute name="CustomerId" type="xs:positiveInteger" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="LastNameType">
<xs:restriction base="xs:string">
<xs:maxLength value="20" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="FirstNameComplexType"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="Title" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType>
</xs:schema>