生成 XML 架构
更新:November 2007
System.Xml.Schema 命名空间中的类映射到万维网联合会 (W3C) XML 架构建议中定义的结构,可以用于在内存中生成 XML 架构。
生成 XML 架构
在下面的代码示例中,SOM API 用于在内存中生成客户 XML 架构。
创建元素和属性
代码示例从下至上生成客户架构,先创建子元素、属性及其相应的类型,然后创建顶级元素。
在下面的代码示例中,客户架构的 FirstName 和 LastName 元素以及 CustomerId 属性使用 SOM 的 XmlSchemaElement 和 XmlSchemaAttribute 类创建。 除了 XmlSchemaElement 和 XmlSchemaAttribute 类的 Name 属性(对应于 XML 架构中 <xs:element /> 和 <xs:attribute /> 元素的“name”属性)之外,架构所允许的所有其他属性(defaultValue、fixedValue、form 等)在 XmlSchemaElement 和 XmlSchemaAttribute 类中均有对应的属性。
' Create the FirstName and LastName elements.
Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
firstNameElement.Name = "FirstName"
Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
lastNameElement.Name = "LastName"
' Create CustomerId attribute.
Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
idAttribute.Name = "CustomerId"
idAttribute.Use = XmlSchemaUse.Required
// Create the FirstName and LastName elements.
XmlSchemaElement firstNameElement = new XmlSchemaElement();
firstNameElement.Name = "FirstName";
XmlSchemaElement lastNameElement = new XmlSchemaElement();
lastNameElement.Name = "LastName";
// Create CustomerId attribute.
XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
idAttribute.Name = "CustomerId";
idAttribute.Use = XmlSchemaUse.Required;
// Create the FirstName and LastName elements.
XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
firstNameElement->Name = "FirstName";
XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
lastNameElement->Name = "LastName";
// Create CustomerId attribute.
XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
idAttribute->Name = "CustomerId";
idAttribute->Use = XmlSchemaUse::Required;
创建架构类型
元素和属性的内容通过其类型定义。 要创建类型属于一种内置架构类型的元素和属性,XmlSchemaElement 或 XmlSchemaAttribute 类的 SchemaTypeName 属性应使用 XmlQualifiedName 类设置该内置类型相应的限定名。 要为元素和属性创建用户定义类型,应使用 XmlSchemaSimpleType 或 XmlSchemaComplexType 类创建新的简单类型或复杂类型。
说明: |
---|
要创建未命名的简单类型或复杂类型,并且属于元素或属性的匿名子级(只有简单类型适用于属性),应将 XmlSchemaElement 或 XmlSchemaAttribute 类的 SchemaType 属性设置为未命名的简单类型或复杂类型,而不是设置 XmlSchemaElement 或 XmlSchemaAttribute 类的 SchemaTypeName 属性。 |
XML 架构允许通过限制从其他简单类型(内置或用户定义)派生的匿名和命名的简单类型,或作为其他简单类型的列表或联合构造的匿名和命名的简单类型。 XmlSchemaSimpleTypeRestriction 类用于通过限制内置 xs:string 类型来创建简单类型。 也可以使用 XmlSchemaSimpleTypeList 或 XmlSchemaSimpleTypeUnion 类创建列表或联合类型。 XmlSchemaSimpleType.Content 属性表明属于简单类型限制、列表还是联合。
在下面的代码示例中,FirstName 元素的类型是内置类型 xs:string,LastName 元素的类型是命名的简单类型,属于内置类型 xs:string 的限制,MaxLength 方面的值为 20,CustomerId 属性的类型是内置类型 xs:positiveInteger。 Customer 元素是匿名的复杂类型,其粒子是 FirstName 和 LastName 元素的序列,其属性包含 CustomerId 属性。
说明: |
---|
也可以使用 XmlSchemaChoice 或 XmlSchemaAll 类作为复杂类型的粒子,以复制 <xs:choice /> 或 <xs:all /> 语义。 |
' Create the simple type for the LastName element.
Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
lastNameType.Name = "LastNameType"
Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
New XmlSchemaSimpleTypeRestriction()
lastNameRestriction.BaseTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
maxLength.Value = "20"
lastNameRestriction.Facets.Add(maxLength)
lastNameType.Content = lastNameRestriction
' Associate the elements and attributes with their types.
' Built-in type.
firstNameElement.SchemaTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' User-defined type.
lastNameElement.SchemaTypeName = _
New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
' Built-in type.
idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
"http://www.w3.org/2001/XMLSchema")
' Create the top-level Customer element.
Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
customerElement.Name = "Customer"
' Create an anonymous complex type for the Customer element.
Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
sequence.Items.Add(firstNameElement)
sequence.Items.Add(lastNameElement)
customerType.Particle = sequence
' Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute)
' Set the SchemaType of the Customer element to
' the anonymous complex type created above.
customerElement.SchemaType = customerType
// Create the simple type for the LastName element.
XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
lastNameType.Name = "LastNameType";
XmlSchemaSimpleTypeRestriction lastNameRestriction =
new XmlSchemaSimpleTypeRestriction();
lastNameRestriction.BaseTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
maxLength.Value = "20";
lastNameRestriction.Facets.Add(maxLength);
lastNameType.Content = lastNameRestriction;
// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement.SchemaTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement.SchemaTypeName =
new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
"http://www.w3.org/2001/XMLSchema");
// Create the top-level Customer element.
XmlSchemaElement customerElement = new XmlSchemaElement();
customerElement.Name = "Customer";
// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType customerType = new XmlSchemaComplexType();
XmlSchemaSequence sequence = new XmlSchemaSequence();
sequence.Items.Add(firstNameElement);
sequence.Items.Add(lastNameElement);
customerType.Particle = sequence;
// Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute);
// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement.SchemaType = customerType;
// Create the simple type for the LastName element.
XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
lastNameType->Name = "LastNameType";
XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
gcnew XmlSchemaSimpleTypeRestriction();
lastNameRestriction->BaseTypeName =
gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
maxLength->Value = "20";
lastNameRestriction->Facets->Add(maxLength);
lastNameType->Content = lastNameRestriction;
// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement->SchemaTypeName =
gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement->SchemaTypeName =
gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
"http://www.w3.org/2001/XMLSchema");
// Create the top-level Customer element.
XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
customerElement->Name = "Customer";
// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
sequence->Items->Add(firstNameElement);
sequence->Items->Add(lastNameElement);
customerType->Particle = sequence;
// Add the CustomerId attribute to the complex type.
customerType->Attributes->Add(idAttribute);
// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement->SchemaType = customerType;
创建和编译架构
现在,已使用 SOM API 在内存中创建了子元素和属性及其相应的类型以及顶级 Customer 元素。 在下面的代码示例中,架构元素使用 XmlSchema 类创建,顶级元素和类型使用 XmlSchema.Items 属性添加到该元素,完整的架构使用 XmlSchemaSet 类进行编译并写入控制台。
' Create an empty schema.
Dim customerSchema As XmlSchema = New XmlSchema()
customerSchema.TargetNamespace = "http://www.tempuri.org"
' Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement)
customerSchema.Items.Add(lastNameType)
' Create an XmlSchemaSet to compile the customer schema.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add(customerSchema)
schemaSet.Compile()
For Each schema As XmlSchema In schemaSet.Schemas()
customerSchema = schema
Next
' Write the complete schema to the Console.
customerSchema.Write(Console.Out)
// Create an empty schema.
XmlSchema customerSchema = new XmlSchema();
customerSchema.TargetNamespace = "http://www.tempuri.org";
// Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement);
customerSchema.Items.Add(lastNameType);
// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add(customerSchema);
schemaSet.Compile();
foreach (XmlSchema schema in schemaSet.Schemas())
{
customerSchema = schema;
}
// Write the complete schema to the Console.
customerSchema.Write(Console.Out);
// Create an empty schema.
XmlSchema^ customerSchema = gcnew XmlSchema();
customerSchema->TargetNamespace = "http://www.tempuri.org";
// Add all top-level element and types to the schema
customerSchema->Items->Add(customerElement);
customerSchema->Items->Add(lastNameType);
// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
schemaSet->Add(customerSchema);
schemaSet->Compile();
for each (XmlSchema^ schema in schemaSet->Schemas())
{
customerSchema = schema;
}
// Write the complete schema to the Console.
customerSchema->Write(Console::Out);
XmlSchemaSet.Compile 方法针对 XML 架构的规则验证客户架构,并使后架构编译属性可用。
说明: |
---|
SOM API 中的所有后架构编译属性与后架构验证信息集不同。 |
添加到 XmlSchemaSet 的 ValidationEventHandler 是一个委托,用于调用回调方法 ValidationCallback 来处理架构验证警告和错误。
以下是完整的代码示例以及写入控制台的客户架构。
Imports System
Imports System.Xml
Imports System.Xml.Schema
Class XmlSchemaCreateExample
Shared Sub Main()
' Create the FirstName and LastName elements.
Dim firstNameElement As XmlSchemaElement = New XmlSchemaElement()
firstNameElement.Name = "FirstName"
Dim lastNameElement As XmlSchemaElement = New XmlSchemaElement()
lastNameElement.Name = "LastName"
' Create CustomerId attribute.
Dim idAttribute As XmlSchemaAttribute = New XmlSchemaAttribute()
idAttribute.Name = "CustomerId"
idAttribute.Use = XmlSchemaUse.Required
' Create the simple type for the LastName element.
Dim lastNameType As XmlSchemaSimpleType = New XmlSchemaSimpleType()
lastNameType.Name = "LastNameType"
Dim lastNameRestriction As XmlSchemaSimpleTypeRestriction = _
New XmlSchemaSimpleTypeRestriction()
lastNameRestriction.BaseTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim maxLength As XmlSchemaMaxLengthFacet = New XmlSchemaMaxLengthFacet()
maxLength.Value = "20"
lastNameRestriction.Facets.Add(maxLength)
lastNameType.Content = lastNameRestriction
' Associate the elements and attributes with their types.
' Built-in type.
firstNameElement.SchemaTypeName = _
New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
' User-defined type.
lastNameElement.SchemaTypeName = _
New XmlQualifiedName("LastNameType", "http://www.tempuri.org")
' Built-in type.
idAttribute.SchemaTypeName = New XmlQualifiedName("positiveInteger", _
"http://www.w3.org/2001/XMLSchema")
' Create the top-level Customer element.
Dim customerElement As XmlSchemaElement = New XmlSchemaElement()
customerElement.Name = "Customer"
' Create an anonymous complex type for the Customer element.
Dim customerType As XmlSchemaComplexType = New XmlSchemaComplexType()
Dim sequence As XmlSchemaSequence = New XmlSchemaSequence()
sequence.Items.Add(firstNameElement)
sequence.Items.Add(lastNameElement)
customerType.Particle = sequence
' Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute)
' Set the SchemaType of the Customer element to
' the anonymous complex type created above.
customerElement.SchemaType = customerType
' Create an empty schema.
Dim customerSchema As XmlSchema = New XmlSchema()
customerSchema.TargetNamespace = "http://www.tempuri.org"
' Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement)
customerSchema.Items.Add(lastNameType)
' Create an XmlSchemaSet to compile the customer schema.
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallback
schemaSet.Add(customerSchema)
schemaSet.Compile()
For Each schema As XmlSchema In schemaSet.Schemas()
customerSchema = schema
Next
' Write the complete schema to the Console.
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
using System;
using System.Xml;
using System.Xml.Schema;
class XmlSchemaCreateExample
{
static void Main(string[] args)
{
// Create the FirstName and LastName elements.
XmlSchemaElement firstNameElement = new XmlSchemaElement();
firstNameElement.Name = "FirstName";
XmlSchemaElement lastNameElement = new XmlSchemaElement();
lastNameElement.Name = "LastName";
// Create CustomerId attribute.
XmlSchemaAttribute idAttribute = new XmlSchemaAttribute();
idAttribute.Name = "CustomerId";
idAttribute.Use = XmlSchemaUse.Required;
// Create the simple type for the LastName element.
XmlSchemaSimpleType lastNameType = new XmlSchemaSimpleType();
lastNameType.Name = "LastNameType";
XmlSchemaSimpleTypeRestriction lastNameRestriction =
new XmlSchemaSimpleTypeRestriction();
lastNameRestriction.BaseTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet maxLength = new XmlSchemaMaxLengthFacet();
maxLength.Value = "20";
lastNameRestriction.Facets.Add(maxLength);
lastNameType.Content = lastNameRestriction;
// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement.SchemaTypeName =
new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement.SchemaTypeName =
new XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute.SchemaTypeName = new XmlQualifiedName("positiveInteger",
"http://www.w3.org/2001/XMLSchema");
// Create the top-level Customer element.
XmlSchemaElement customerElement = new XmlSchemaElement();
customerElement.Name = "Customer";
// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType customerType = new XmlSchemaComplexType();
XmlSchemaSequence sequence = new XmlSchemaSequence();
sequence.Items.Add(firstNameElement);
sequence.Items.Add(lastNameElement);
customerType.Particle = sequence;
// Add the CustomerId attribute to the complex type.
customerType.Attributes.Add(idAttribute);
// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement.SchemaType = customerType;
// Create an empty schema.
XmlSchema customerSchema = new XmlSchema();
customerSchema.TargetNamespace = "http://www.tempuri.org";
// Add all top-level element and types to the schema
customerSchema.Items.Add(customerElement);
customerSchema.Items.Add(lastNameType);
// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
schemaSet.Add(customerSchema);
schemaSet.Compile();
foreach (XmlSchema schema in schemaSet.Schemas())
{
customerSchema = schema;
}
// Write the complete schema to the Console.
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);
}
}
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
ref class XmlSchemaCreateExample
{
public:
static void Main()
{
// Create the FirstName and LastName elements.
XmlSchemaElement^ firstNameElement = gcnew XmlSchemaElement();
firstNameElement->Name = "FirstName";
XmlSchemaElement^ lastNameElement = gcnew XmlSchemaElement();
lastNameElement->Name = "LastName";
// Create CustomerId attribute.
XmlSchemaAttribute^ idAttribute = gcnew XmlSchemaAttribute();
idAttribute->Name = "CustomerId";
idAttribute->Use = XmlSchemaUse::Required;
// Create the simple type for the LastName element.
XmlSchemaSimpleType^ lastNameType = gcnew XmlSchemaSimpleType();
lastNameType->Name = "LastNameType";
XmlSchemaSimpleTypeRestriction^ lastNameRestriction =
gcnew XmlSchemaSimpleTypeRestriction();
lastNameRestriction->BaseTypeName =
gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaMaxLengthFacet^ maxLength = gcnew XmlSchemaMaxLengthFacet();
maxLength->Value = "20";
lastNameRestriction->Facets->Add(maxLength);
lastNameType->Content = lastNameRestriction;
// Associate the elements and attributes with their types.
// Built-in type.
firstNameElement->SchemaTypeName =
gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// User-defined type.
lastNameElement->SchemaTypeName =
gcnew XmlQualifiedName("LastNameType", "http://www.tempuri.org");
// Built-in type.
idAttribute->SchemaTypeName = gcnew XmlQualifiedName("positiveInteger",
"http://www.w3.org/2001/XMLSchema");
// Create the top-level Customer element.
XmlSchemaElement^ customerElement = gcnew XmlSchemaElement();
customerElement->Name = "Customer";
// Create an anonymous complex type for the Customer element.
XmlSchemaComplexType^ customerType = gcnew XmlSchemaComplexType();
XmlSchemaSequence^ sequence = gcnew XmlSchemaSequence();
sequence->Items->Add(firstNameElement);
sequence->Items->Add(lastNameElement);
customerType->Particle = sequence;
// Add the CustomerId attribute to the complex type.
customerType->Attributes->Add(idAttribute);
// Set the SchemaType of the Customer element to
// the anonymous complex type created above.
customerElement->SchemaType = customerType;
// Create an empty schema.
XmlSchema^ customerSchema = gcnew XmlSchema();
customerSchema->TargetNamespace = "http://www.tempuri.org";
// Add all top-level element and types to the schema
customerSchema->Items->Add(customerElement);
customerSchema->Items->Add(lastNameType);
// Create an XmlSchemaSet to compile the customer schema.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallback);
schemaSet->Add(customerSchema);
schemaSet->Compile();
for each (XmlSchema^ schema in schemaSet->Schemas())
{
customerSchema = schema;
}
// Write the complete schema to the Console.
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);
}
};
int main()
{
XmlSchemaCreateExample::Main();
return 0;
}
<?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: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>