Building and Editing XML Schema
The classes in the System.Xml.Schema namespace map to the structures defined in the World Wide Web Consortium (W3C) XML Schema Recommendation. Because some schema elements can have attributes and can contain child elements, the corresponding classes in the Schema Object Model (SOM) have properties that can contain attributes and child elements.
You can manipulate a schema in memory by traversing the SOM and setting or unsetting properties, to correspond with creating, inserting, or deleting nodes and attributes in a schema document. According to the W3C XML Schema Recommendation, the content model of a schema is given as:
Content: ((include | import | redefine | annotation)*, (((simpleType | complexType | group | attributeGroup) | element | attribute | notation), annotation*)*).
While adding new items to the SOM, the first set of items in the content model (include, import, and redefine) should be added to the Schema.Includes collection and the remaining items in the content model should be added to the Schema.Items collection.
The following example creates an empty XML Schema, modifies the schema to contain an element, Price
, that has a type of xs:integer
, then modifies the schema to nest a simpleType element that has a restriction base of xs:positiveInteger
.
Imports System.IO
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Text
Imports Microsoft.VisualBasic
Public Class EditSOM
Public Shared Sub ValidationCallbackOne(sender As Object, 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 ' ValidationCallbackOne
Public Shared Sub Main()
Dim schema As New XmlSchema()
Console.WriteLine("Printing empty schema...")
schema.Compile(AddressOf ValidationCallbackOne)
schema.Write(Console.Out)
Console.WriteLine(ControlChars.CrLf & ControlChars.CrLf)
Dim priceElem As New XmlSchemaElement()
priceElem.Name = "Price"
priceElem.SchemaTypeName = New XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema")
schema.Items.Add(priceElem)
schema.Compile(AddressOf ValidationCallbackOne)
Console.WriteLine("Printing modified schema containing element with type=""integer""")
schema.Write(Console.Out)
Console.WriteLine(ControlChars.CrLf & ControlChars.CrLf)
priceElem.SchemaTypeName = Nothing
Dim PriceType As New XmlSchemaSimpleType()
Dim PriceRestriction As New XmlSchemaSimpleTypeRestriction()
PriceRestriction.BaseTypeName = New XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema")
PriceType.Content = PriceRestriction
Dim maxExclusive As New XmlSchemaMaxExclusiveFacet()
maxExclusive.Value = "100"
PriceRestriction.Facets.Add(maxExclusive)
priceElem.SchemaType = PriceType
schema.Compile(AddressOf ValidationCallbackOne)
Console.WriteLine("Printing modified schema containing element with nested simpleType")
schema.Write(Console.Out)
End Sub 'Main
End Class 'EditSOM
' Main(string[])
' EditSOM
[C#]
using System.IO;
using System;
using System.Xml;
using System.Xml.Schema;
using System.Text;
public class EditSOM {
public static void ValidationCallbackOne(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);
}
public static void Main(string[] args){
XmlSchema schema = new XmlSchema();
Console.WriteLine("Printing empty schema...");
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
schema.Write(Console.Out);
Console.WriteLine("\n\n");
XmlSchemaElement priceElem = new XmlSchemaElement();
priceElem.Name = "Price";
priceElem.SchemaTypeName = new XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema");
schema.Items.Add(priceElem);
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
Console.WriteLine("Printing modified schema containing element with type=\"integer\"");
schema.Write(Console.Out);
Console.WriteLine("\n\n");
priceElem.SchemaTypeName = null;
XmlSchemaSimpleType PriceType = new XmlSchemaSimpleType();
XmlSchemaSimpleTypeRestriction PriceRestriction =
new XmlSchemaSimpleTypeRestriction();
PriceRestriction.BaseTypeName =
new XmlQualifiedName("positiveInteger", "http://www.w3.org/2001/XMLSchema");
PriceType.Content = PriceRestriction;
XmlSchemaMaxExclusiveFacet maxExclusive =
new XmlSchemaMaxExclusiveFacet();
maxExclusive.Value = "100";
PriceRestriction.Facets.Add(maxExclusive);
priceElem.SchemaType = PriceType;
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
Console.WriteLine("Printing modified schema containing element with nested simpleType");
schema.Write(Console.Out);
}/* Main(string[]) */
}// EditSOM
The following output is generated from the preceding code example.
Printing empty schema...
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
</schema>
Printing modified schema containing element with type="integer"
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Price" type="integer"/>
</schema>
Printing modified schema containing element with nested simpleType
<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:apo="http://www.example.com/PO1">
<element name="Price">
<simpleType>
<restiction base="positiveInteger">
<maxExclusive="100" />
</restriction>
</simpleType>
</element>
</schema>
See Also
XML Schema Object Model (SOM) | XML Schema Reference (XSD) | System.Xml.Schema Namespace