XML-schema's bewerken
Het bewerken van een XML-schema is een van de belangrijkste functies van het Schema Object Model (SOM). Alle preschemacompilatie-eigenschappen van de SOM kunnen worden gebruikt om de bestaande waarden in een XML-schema te wijzigen. Het XML-schema kan vervolgens opnieuw worden gecompileerd om de wijzigingen weer te geven.
De eerste stap bij het bewerken van een schema dat in de SOM is geladen, is het doorlopen van het schema. U moet bekend zijn met het doorlopen van een schema met behulp van de SOM-API voordat u probeert een schema te bewerken. U moet ook bekend zijn met de eigenschappen voor pre- en schemacompilatie van de Post-Schema-Compilation-Infoset (PSCI).
Een XML-schema bewerken
In deze sectie worden twee codevoorbeelden gegeven, die beide het klantschema bewerken dat is gemaakt in het onderwerp XML-schema's bouwen. Het eerste codevoorbeeld voegt een nieuw PhoneNumber
element toe aan het Customer
element en het tweede codevoorbeeld voegt een nieuw Title
kenmerk toe aan het FirstName
element. In het eerste voorbeeld wordt ook de verzameling post-schemacompilatie gebruikt als middel om het klantschema te doorlopen, terwijl in het tweede codevoorbeeld de verzameling pre-schemacompilatie XmlSchema.ElementsXmlSchema.Items wordt gebruikt.
voorbeeld van Telefoon Getalelement
In dit eerste codevoorbeeld wordt een nieuw PhoneNumber
element toegevoegd aan het element van het Customer
klantschema. In het codevoorbeeld wordt het klantschema in de volgende stappen bewerkt.
Voegt het klantschema toe aan een nieuw XmlSchemaSet object en compileert het vervolgens. Eventuele schemavalidatiewaarschuwingen en fouten die zijn opgetreden bij het lezen of compileren van het schema, worden verwerkt door de ValidationEventHandler gemachtigde.
Haalt het gecompileerde XmlSchema object op van de XmlSchemaSet eigenschap door de Schemas eigenschap te herhalen. Omdat het schema is gecompileerd, zijn de eigenschappen Post-Schema-Compilation-Infoset (PSCI) toegankelijk.
Hiermee maakt u het
PhoneNumber
element met behulp van de XmlSchemaElement klasse, dexs:string
eenvoudige typebeperking met behulp van de XmlSchemaSimpleType en XmlSchemaSimpleTypeRestriction klassen, voegt u een patroon facet toe aan de Facets eigenschap van de beperking en voegt u de beperking toe aan de Content eigenschap van het eenvoudige type en het eenvoudige type aan het SchemaTypePhoneNumber
element.Herhaalt elk XmlSchemaElement item in de Values verzameling van de verzameling na schemacompilatie XmlSchema.Elements .
Als het QualifiedName element is
"Customer"
, haalt u het complexe type van hetCustomer
element op met behulp van de XmlSchemaComplexType klasse en het sequentiedeeltje van het complexe type met behulp van de XmlSchemaSequence klasse.Voegt het nieuwe
PhoneNumber
element toe aan de reeks die de bestaandeFirstName
elementenLastName
bevat met behulp van de verzameling preschemacompilatie Items van de reeks.Ten slotte worden het gewijzigde XmlSchema object opnieuw verwerkt en gecompileerd met behulp van de Reprocess en Compile methoden van de XmlSchemaSet klasse en wordt het naar de console geschreven.
Hier volgt het volledige codevoorbeeld.
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
ref class XmlSchemaEditExample
{
public:
static void 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.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew 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 = nullptr;
for each (XmlSchema^ schema in schemaSet->Schemas())
{
customerSchema = schema;
}
// Create the PhoneNumber element.
XmlSchemaElement^ phoneElement = gcnew XmlSchemaElement();
phoneElement->Name = "PhoneNumber";
// Create the xs:string simple type restriction.
XmlSchemaSimpleType^ phoneType = gcnew XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction^ restriction =
gcnew XmlSchemaSimpleTypeRestriction();
restriction->BaseTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
// Add a pattern facet to the restriction.
XmlSchemaPatternFacet^ phonePattern = gcnew 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 (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 =
dynamic_cast<XmlSchemaComplexType^>(element->ElementSchemaType);
XmlSchemaSequence^ sequence =
dynamic_cast<XmlSchemaSequence^>(customerType->Particle);
// 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);
}
};
int main()
{
XmlSchemaEditExample::Main();
return 0;
}
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
Hier volgt het aangepaste klantschema dat is gemaakt in het onderwerp XML-schema's bouwen.
<?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>
Voorbeeld van titelkenmerk
In dit tweede codevoorbeeld wordt een nieuw Title
kenmerk toegevoegd aan het element van het FirstName
klantschema. In het eerste codevoorbeeld is xs:string
het type element FirstName
. Het element moet worden gewijzigd in een complex type met een eenvoudige inhoudsextensie con tentmodus l om het FirstName
element een kenmerk samen met tekenreeksinhoud te hebben.
In het codevoorbeeld wordt het klantschema in de volgende stappen bewerkt.
Voegt het klantschema toe aan een nieuw XmlSchemaSet object en compileert het vervolgens. Eventuele schemavalidatiewaarschuwingen en fouten die zijn opgetreden bij het lezen of compileren van het schema, worden verwerkt door de ValidationEventHandler gemachtigde.
Haalt het gecompileerde XmlSchema object op van de XmlSchemaSet eigenschap door de Schemas eigenschap te herhalen. Omdat het schema is gecompileerd, zijn de eigenschappen Post-Schema-Compilation-Infoset (PSCI) toegankelijk.
Hiermee maakt u een nieuw complex type voor het
FirstName
element met behulp van de XmlSchemaComplexType klasse.Hiermee maakt u een nieuwe eenvoudige inhoudsextensie, met een basistype , met behulp van
xs:string
de XmlSchemaSimpleContent en XmlSchemaSimpleContentExtension klassen.Hiermee maakt u het nieuwe
Title
kenmerk met behulp vanxs:string
de XmlSchemaAttribute klasse en SchemaTypeName voegt u het kenmerk toe aan de eenvoudige inhoudsextensie.Hiermee stelt u de con tentmodus l van de eenvoudige inhoud in op de eenvoudige inhoudsextensie en de con tentmodus l van het complexe type op de eenvoudige inhoud.
Hiermee voegt u het nieuwe complexe type toe aan de verzameling pre-schemacompilatie XmlSchema.Items .
Itereert elke XmlSchemaObject verzameling in de verzameling preschemacompilatie XmlSchema.Items .
Notitie
Omdat het FirstName
element geen globaal element in het schema is, is het niet beschikbaar in de XmlSchema.Items of XmlSchema.Elements verzamelingen. In het codevoorbeeld wordt het FirstName
element gevonden door eerst het Customer
element te zoeken.
Het eerste codevoorbeeld doorkruist het schema met behulp van de verzameling na schemacompilatie XmlSchema.Elements . In dit voorbeeld wordt de verzameling pre-schemacompilatie XmlSchema.Items gebruikt om het schema te doorlopen. Hoewel beide verzamelingen toegang bieden tot de globale elementen in het schema, is het doorlopen van de Items verzameling tijdrovender omdat u alle globale elementen in het schema moet herhalen en geen PSCI-eigenschappen heeft. De PSCI-verzamelingen (XmlSchema.Elements, XmlSchema.Attributesenzovoort XmlSchema.SchemaTypes) bieden directe toegang tot hun globale elementen, kenmerken en typen en hun PSCI-eigenschappen.
Als het XmlSchemaObject een element is waarvan het is
"Customer"
, QualifiedName haalt het complexe type van hetCustomer
element op met behulp van de XmlSchemaComplexType klasse en het sequentiedeeltje van het complexe type met behulp van de XmlSchemaSequence klasse.Itereert elke XmlSchemaParticle verzameling in de verzameling preschemacompilatie XmlSchemaSequence.Items .
Als het XmlSchemaParticle een element is, wie QualifiedName is
"FirstName"
, stelt u het SchemaTypeNameFirstName
element in op het nieuweFirstName
complexe type.Ten slotte worden het gewijzigde XmlSchema object opnieuw verwerkt en gecompileerd met behulp van de Reprocess en Compile methoden van de XmlSchemaSet klasse en wordt het naar de console geschreven.
Hier volgt het volledige codevoorbeeld.
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
ref class XmlSchemaEditExample
{
public:
static void 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.
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
schemaSet->ValidationEventHandler += gcnew 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 = nullptr;
for each (XmlSchema^ schema in schemaSet->Schemas())
{
customerSchema = schema;
}
// Create a complex type for the FirstName element.
XmlSchemaComplexType^ complexType = gcnew XmlSchemaComplexType();
complexType->Name = "FirstNameComplexType";
// Create a simple content extension with a base type of xs:string.
XmlSchemaSimpleContent^ simpleContent = gcnew XmlSchemaSimpleContent();
XmlSchemaSimpleContentExtension^ simpleContentExtension =
gcnew XmlSchemaSimpleContentExtension();
simpleContentExtension->BaseTypeName =
gcnew 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 = gcnew XmlSchemaAttribute();
attribute->Name = "Title";
attribute->SchemaTypeName =
gcnew 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 (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::typeid == XmlSchemaElement::typeid)
{
XmlSchemaElement^ element = dynamic_cast<XmlSchemaElement^>(schemaObject);
if (element->QualifiedName->Name->Equals("Customer"))
{
XmlSchemaComplexType^ customerType =
dynamic_cast<XmlSchemaComplexType^>(element->ElementSchemaType);
XmlSchemaSequence^ sequence =
dynamic_cast<XmlSchemaSequence^>(customerType->Particle);
// Iterate over each XmlSchemaParticle in the pre-schema-compilation
// Items property.
for each (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::typeid == XmlSchemaElement::typeid)
{
XmlSchemaElement^ childElement =
dynamic_cast<XmlSchemaElement^>(particle);
if (childElement->Name->Equals("FirstName"))
{
childElement->SchemaTypeName =
gcnew 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);
}
};
int main()
{
XmlSchemaEditExample::Main();
return 0;
}
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
Hier volgt het aangepaste klantschema dat is gemaakt in het onderwerp XML-schema's bouwen.
<?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>