編輯 XML 架構是架構物件模型 (SOM) 最重要的功能之一。 SOM 的所有架構前編譯屬性都可以用來變更 XML 架構中的現有值。 接著可以重新編譯 XML 架構,以反映變更。
編輯載入至 SOM 的架構的第一個步驟是周遊架構。 在嘗試編輯架構之前,您應該先熟悉使用 SOM API 周遊架構。 您也應該熟悉 Post-Schema-Compilation-Infoset (PSCI) 的前置和後架構編譯屬性。
編輯 XML 架構
在本節中,會提供兩個程式碼範例,這兩個範例都會編輯 在建置 XML 架構 主題中建立的客戶架構。 第一個程式代碼範例會將新 PhoneNumber 元素加入至 Customer 專案,而第二個程式代碼範例會將新的 Title 屬性加入至 FirstName 專案。 第一個範例也會使用後架構編譯 XmlSchema.Elements 集合作為周遊客戶架構的方法,而第二個程式代碼範例則使用預先架構編譯 XmlSchema.Items 集合。
PhoneNumber 元素範例
第一個程式代碼範例會將新 PhoneNumber 元素新增至 Customer 客戶架構的 元素。 程式代碼範例會在下列步驟中編輯客戶架構。
將客戶架構新增至新的 XmlSchemaSet 物件,然後加以編譯。 委派函式 ValidationEventHandler 會處理在讀取或編譯結構時出現的任何結構驗證警告和錯誤。
從 XmlSchema 中逐一查看 XmlSchemaSet 屬性以擷取編譯的Schemas物件。 因為架構已編譯,因此可以存取后架構Compilation-Infoset (PSCI) 屬性。
PhoneNumber使用 類別建立 專案、XmlSchemaElement使用xs:stringXmlSchemaSimpleType 和 XmlSchemaSimpleTypeRestriction 類別的簡單型別限制、將模式 Facet 加入至限制的 屬性,並將限制新增至Facets簡單型別的 屬性,並將簡單型別的限制新增至 ContentSchemaTypePhoneNumber專案的 。逐一XmlSchemaElement查看後架構編譯Values集合集合中的每個 XmlSchema.Elements 。
QualifiedName如果 專案的 是
"Customer",則使用CustomerXmlSchemaComplexType 類別取得專案的複雜型XmlSchemaSequence別,並使用 類別取得複雜型別的序列粒子。使用序列的架構編譯
PhoneNumber前集合,將新FirstName專案加入至包含現有LastName和 Items 元素的序列。最後,使用 XmlSchema 類別的 Reprocess 和 Compile 方法重新處理和編譯修改XmlSchemaSet的物件,並將它寫入主控台。
以下是完整的程式代碼範例。
using System;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaEditExample
{
static void Main(string[] args)
{
// Add the customer schema to a new XmlSchemaSet and compile it.
// Any schema validation warnings and errors encountered reading or
// compiling the schema are handled by the ValidationEventHandler delegate.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.tempuri.org", "customer.xsd");
schemaSet.Compile();
// Retrieve the compiled XmlSchema object from the XmlSchemaSet
// by iterating over the Schemas property.
XmlSchema customerSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
customerSchema = schema;
}
// Create the PhoneNumber element.
XmlSchemaElement phoneElement = new XmlSchemaElement();
phoneElement.Name = "PhoneNumber";
// Create the xs:string simple type restriction.
XmlSchemaSimpleType phoneType = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction restriction =
new XmlSchemaSimpleTypeRestriction();
restriction.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// Add a pattern facet to the restriction.
XmlSchemaPatternFacet phonePattern = new XmlSchemaPatternFacet();
phonePattern.Value = "\\d{3}-\\d{3}-\\d(4)";
restriction.Facets.Add(phonePattern);
// Add the restriction to the Content property of the simple type
// and the simple type to the SchemaType of the PhoneNumber element.
phoneType.Content = restriction;
phoneElement.SchemaType = phoneType;
// Iterate over each XmlSchemaElement in the Values collection
// of the Elements property.
foreach (XmlSchemaElement element in customerSchema.Elements.Values)
{
// If the qualified name of the element is "Customer",
// get the complex type of the Customer element
// and the sequence particle of the complex type.
if (element.QualifiedName.Name.Equals("Customer"))
{
XmlSchemaComplexType customerType =
element.ElementSchemaType as XmlSchemaComplexType;
XmlSchemaSequence sequence =
customerType.Particle as XmlSchemaSequence;
// Add the new PhoneNumber element to the sequence.
sequence.Items.Add(phoneElement);
}
}
// Reprocess and compile the modified XmlSchema object and write it to the console.
schemaSet.Reprocess(customerSchema);
schemaSet.Compile();
customerSchema.Write(Console.Out);
}
static void ValidationCallback(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.Write("WARNING: ");
else if (args.Severity == XmlSeverityType.Error)
Console.Write("ERROR: ");
Console.WriteLine(args.Message);
}
}
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaEditExample
Shared Sub Main()
' Add the customer schema to a new XmlSchemaSet and compile it.
' Any schema validation warnings and errors encountered reading or
' compiling the schema are handled by the ValidationEventHandler delegate.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add("http://www.tempuri.org", "customer.xsd")
schemaSet.Compile()
' Retrieve the compiled XmlSchema object from the XmlSchemaSet
' by iterating over the Schemas property.
Dim customerSchema As XmlSchema = Nothing
For Each schema As XmlSchema In schemaSet.Schemas()
customerSchema = schema
Next
' Create the PhoneNumber element.
Dim phoneElement As XmlSchemaElement = New XmlSchemaElement()
phoneElement.Name = "PhoneNumber"
' Create the xs:string simple type restriction.
Dim phoneType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
Dim restriction As XmlSchemaSimpleTypeRestriction = _
New XmlSchemaSimpleTypeRestriction()
restriction.BaseTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' Add a pattern facet to the restriction.
Dim phonePattern As XmlSchemaPatternFacet = New XmlSchemaPatternFacet()
phonePattern.Value = "\\d{3}-\\d{3}-\\d(4)"
restriction.Facets.Add(phonePattern)
' Add the restriction to the Content property of the simple type
' and the simple type to the SchemaType of the PhoneNumber element.
phoneType.Content = restriction
phoneElement.SchemaType = phoneType
' Iterate over each XmlSchemaElement in the Values collection
' of the Elements property.
For Each element As XmlSchemaElement In customerSchema.Elements.Values
' If the qualified name of the element is "Customer",
' get the complex type of the Customer element
' and the sequence particle of the complex type.
If element.QualifiedName.Name.Equals("Customer") Then
Dim customerType As XmlSchemaComplexType = _
CType(element.ElementSchemaType, XmlSchemaComplexType)
Dim sequence As XmlSchemaSequence = _
CType(customerType.Particle, XmlSchemaSequence)
' Add the new PhoneNumber element to the sequence.
sequence.Items.Add(phoneElement)
End If
Next
' Reprocess and compile the modified XmlSchema object and write it to the console.
schemaSet.Reprocess(customerSchema)
schemaSet.Compile()
customerSchema.Write(Console.Out)
End Sub
Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Else
If args.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
End If
End If
Console.WriteLine(args.Message)
End Sub
End Class
以下是 在建置 XML 架構 主題中建立的已修改客戶架構。
<?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>
Title 屬性範例
第二個程式代碼範例會將新的 Title 屬性新增至 FirstName 客戶架構的 元素。 在第一個程式代碼範例中,元素的類型 FirstName 為 xs:string。 若要讓 FirstName 專案具有屬性以及字串內容,其類型必須變更為具有簡單內容延伸內容模型的複雜類型。
程式代碼範例會在下列步驟中編輯客戶架構。
將客戶架構新增至新的 XmlSchemaSet 物件,然後加以編譯。 委派函式 ValidationEventHandler 會處理在讀取或編譯結構時出現的任何結構驗證警告和錯誤。
從 XmlSchema 中逐一查看 XmlSchemaSet 屬性以擷取編譯的Schemas物件。 因為架構已編譯,因此可以存取后架構Compilation-Infoset (PSCI) 屬性。
使用
FirstName類別為XmlSchemaComplexType專案建立新的複雜型別。使用
xs:string與 XmlSchemaSimpleContent 類別,建立新的簡單內容延伸模組,其基底類型為 XmlSchemaSimpleContentExtension。使用
Title類別建立新的XmlSchemaAttribute屬性,並將 SchemaTypeNamexs:string屬性加入至簡單的內容延伸模組。將簡單內容的內容模型設定為簡單內容延伸模組,並將複雜類型的內容模型設定為簡單內容。
將新的複雜類型加入至架構 XmlSchema.Items 編譯前集合。
逐一 XmlSchemaObject 查看預先架構編譯 XmlSchema.Items 集合中的每個 。
備註
因為專案FirstName不是架構中的全域專案,所以 或 XmlSchema.Items 集合中XmlSchema.Elements無法使用。 程式代碼範例會先找出 元素,FirstName以找出 Customer 元素。
第一個程式代碼範例使用後架構編譯 XmlSchema.Elements 集合周游架構。 在此範例中,會使用預先架構編譯 XmlSchema.Items 集合來周遊架構。 雖然這兩個集合都提供架構中全域元素的存取權,但逐一查看 Items 集合會比較耗時,因為您必須逐一查看架構中的所有全域元素,而且沒有任何 PSCI 屬性。 PSCI 集合 (XmlSchema.Elements、 XmlSchema.AttributesXmlSchema.SchemaTypes、 等) 提供其全域元素、屬性和型別及其 PSCI 屬性的直接存取權。
XmlSchemaObject如果 是 專案,其 QualifiedName 為
"Customer",則會使用CustomerXmlSchemaComplexType 類別取得專案的複雜型XmlSchemaSequence別,並使用 類別取得複雜型別的序列粒子。逐一 XmlSchemaParticle 查看預先架構編譯 XmlSchemaSequence.Items 集合中的每個 。
XmlSchemaParticle如果 是 專案,其 QualifiedName 為
"FirstName",會將 SchemaTypeName 項目的 設定FirstName為新的FirstName複雜型別。最後,使用 XmlSchema 類別的 Reprocess 和 Compile 方法重新處理和編譯修改XmlSchemaSet的物件,並將它寫入主控台。
以下是完整的程式代碼範例。
using System;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaEditExample
{
static void Main(string[] args)
{
// Add the customer schema to a new XmlSchemaSet and compile it.
// Any schema validation warnings and errors encountered reading or
// compiling the schema are handled by the ValidationEventHandler delegate.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add("http://www.tempuri.org", "customer.xsd");
schemaSet.Compile();
// Retrieve the compiled XmlSchema object from the XmlSchemaSet
// by iterating over the Schemas property.
XmlSchema customerSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
customerSchema = schema;
}
// Create a complex type for the FirstName element.
XmlSchemaComplexType complexType = new XmlSchemaComplexType();
complexType.Name = "FirstNameComplexType";
// Create a simple content extension with a base type of xs:string.
XmlSchemaSimpleContent simpleContent = new XmlSchemaSimpleContent();
XmlSchemaSimpleContentExtension simpleContentExtension =
new XmlSchemaSimpleContentExtension();
simpleContentExtension.BaseTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// Create the new Title attribute with a SchemaTypeName of xs:string
// and add it to the simple content extension.
XmlSchemaAttribute attribute = new XmlSchemaAttribute();
attribute.Name = "Title";
attribute.SchemaTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
simpleContentExtension.Attributes.Add(attribute);
// Set the content model of the simple content to the simple content extension
// and the content model of the complex type to the simple content.
simpleContent.Content = simpleContentExtension;
complexType.ContentModel = simpleContent;
// Add the new complex type to the pre-schema-compilation Items collection.
customerSchema.Items.Add(complexType);
// Iterate over each XmlSchemaObject in the pre-schema-compilation
// Items collection.
foreach (XmlSchemaObject schemaObject in customerSchema.Items)
{
// If the XmlSchemaObject is an element, whose QualifiedName
// is "Customer", get the complex type of the Customer element
// and the sequence particle of the complex type.
if (schemaObject is XmlSchemaElement)
{
XmlSchemaElement element = schemaObject as XmlSchemaElement;
if (element.QualifiedName.Name.Equals("Customer"))
{
XmlSchemaComplexType customerType =
element.ElementSchemaType as XmlSchemaComplexType;
XmlSchemaSequence sequence =
customerType.Particle as XmlSchemaSequence;
// Iterate over each XmlSchemaParticle in the pre-schema-compilation
// Items property.
foreach (XmlSchemaParticle particle in sequence.Items)
{
// If the XmlSchemaParticle is an element, who's QualifiedName
// is "FirstName", set the SchemaTypeName of the FirstName element
// to the new FirstName complex type.
if (particle is XmlSchemaElement)
{
XmlSchemaElement childElement =
particle as XmlSchemaElement;
if (childElement.Name.Equals("FirstName"))
{
childElement.SchemaTypeName =
new XmlQualifiedName("FirstNameComplexType",
"http://www.tempuri.org");
}
}
}
}
}
}
// Reprocess and compile the modified XmlSchema object and write it to the console.
schemaSet.Reprocess(customerSchema);
schemaSet.Compile();
customerSchema.Write(Console.Out);
}
static void ValidationCallback(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
Console.Write("WARNING: ");
else if (args.Severity == XmlSeverityType.Error)
Console.Write("ERROR: ");
Console.WriteLine(args.Message);
}
}
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaEditExample
Shared Sub Main()
' Add the customer schema to a new XmlSchemaSet and compile it.
' Any schema validation warnings and errors encountered reading or
' compiling the schema are handled by the ValidationEventHandler delegate.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add("http://www.tempuri.org", "customer.xsd")
schemaSet.Compile()
' Retrieve the compiled XmlSchema object from the XmlSchemaSet
' by iterating over the Schemas property.
Dim customerSchema As XmlSchema = Nothing
For Each schema As XmlSchema In schemaSet.Schemas()
customerSchema = schema
Next
' Create a complex type for the FirstName element.
Dim complexType As XmlSchemaComplexType = New XmlSchemaComplexType()
complexType.Name = "FirstNameComplexType"
' Create a simple content extension with a base type of xs:string.
Dim simpleContent As XmlSchemaSimpleContent = New XmlSchemaSimpleContent()
Dim simpleContentExtension As XmlSchemaSimpleContentExtension = _
New XmlSchemaSimpleContentExtension()
simpleContentExtension.BaseTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' Create the new Title attribute with a SchemaTypeName of xs:string
' and add it to the simple content extension.
Dim attribute As XmlSchemaAttribute = New XmlSchemaAttribute()
attribute.Name = "Title"
attribute.SchemaTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
simpleContentExtension.Attributes.Add(attribute)
' Set the content model of the simple content to the simple content extension
' and the content model of the complex type to the simple content.
simpleContent.Content = simpleContentExtension
complexType.ContentModel = simpleContent
' Add the new complex type to the pre-schema-compilation Items collection.
customerSchema.Items.Add(complexType)
' Iterate over each XmlSchemaObject in the pre-schema-compilation
' Items collection.
For Each schemaObject As XmlSchemaObject In customerSchema.Items
' If the XmlSchemaObject is an element, whose QualifiedName
' is "Customer", get the complex type of the Customer element
' and the sequence particle of the complex type.
If schemaObject.GetType() Is GetType(XmlSchemaElement) Then
Dim element As XmlSchemaElement = CType(schemaObject, XmlSchemaElement)
If (element.QualifiedName.Name.Equals("Customer")) Then
Dim customerType As XmlSchemaComplexType = _
CType(element.ElementSchemaType, XmlSchemaComplexType)
Dim sequence As XmlSchemaSequence = _
CType(customerType.Particle, XmlSchemaSequence)
' Iterate over each XmlSchemaParticle in the pre-schema-compilation
' Items property.
For Each particle As XmlSchemaParticle In sequence.Items
' If the XmlSchemaParticle is an element, who's QualifiedName
' is "FirstName", set the SchemaTypeName of the FirstName element
' to the new FirstName complex type.
If particle.GetType() Is GetType(XmlSchemaElement) Then
Dim childElement As XmlSchemaElement = _
CType(particle, XmlSchemaElement)
If childElement.Name.Equals("FirstName") Then
childElement.SchemaTypeName = _
New XmlQualifiedName("FirstNameComplexType", _
"http://www.tempuri.org")
End If
End If
Next
End If
End If
Next
' Reprocess and compile the modified XmlSchema object and write it to the console.
schemaSet.Reprocess(customerSchema)
schemaSet.Compile()
customerSchema.Write(Console.Out)
End Sub
Shared Sub ValidationCallback(ByVal sender As Object, ByVal args As ValidationEventArgs)
If args.Severity = XmlSeverityType.Warning Then
Console.Write("WARNING: ")
Else
If args.Severity = XmlSeverityType.Error Then
Console.Write("ERROR: ")
End If
End If
Console.WriteLine(args.Message)
End Sub
End Class
以下是 在建置 XML 架構 主題中建立的已修改客戶架構。
<?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>