XmlSchemaSimpleType Class
.NET Framework 3.0
Represents the simpleType element for simple content from XML Schema as specified by the . This class defines a simple type. Simple types can specify information and constraints for the value of attributes or elements with text-only content.
Namespace: System.Xml.Schema
Assembly: System.Xml (in system.xml.dll)
Assembly: System.Xml (in system.xml.dll)
The following example shows the use of the XmlSchemaSimpleType class.
using System; using System.Xml; using System.Xml.Schema; class XMLSchemaExamples { public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:simpleType name="LotteryNumber"> XmlSchemaSimpleType LotteryNumberType = new XmlSchemaSimpleType(); LotteryNumberType.Name = "LotteryNumber"; // <xs:restriction base="xs:int"> XmlSchemaSimpleTypeRestriction LotteryNumberRestriction = new XmlSchemaSimpleTypeRestriction(); LotteryNumberRestriction.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema"); // <xs:minInclusive value="1"/> XmlSchemaMinInclusiveFacet minInclusive = new XmlSchemaMinInclusiveFacet(); minInclusive.Value = "1"; LotteryNumberRestriction.Facets.Add(minInclusive); // <xs:maxInclusive value="99"/> XmlSchemaMaxInclusiveFacet maxInclusive = new XmlSchemaMaxInclusiveFacet(); maxInclusive.Value = "99"; LotteryNumberRestriction.Facets.Add(maxInclusive); LotteryNumberType.Content = LotteryNumberRestriction; schema.Items.Add(LotteryNumberType); // <xs:simpleType name="LotteryNumberList"> XmlSchemaSimpleType LotteryNumberListType = new XmlSchemaSimpleType(); LotteryNumberListType.Name = "LotteryNumberList"; // <xs:list itemType="LotteryNumber"/> XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList(); list.ItemTypeName = new XmlQualifiedName("LotteryNumber", ""); LotteryNumberListType.Content = list; schema.Items.Add(LotteryNumberListType); // <xs:simpleType name="LotteryNumbers"> XmlSchemaSimpleType LotteryNumbersType = new XmlSchemaSimpleType(); LotteryNumbersType.Name = "LotteryNumbers"; // <xs:restriction base="LotteryNumberList"> XmlSchemaSimpleTypeRestriction LotteryNumbersRestriction = new XmlSchemaSimpleTypeRestriction(); LotteryNumbersRestriction.BaseTypeName = new XmlQualifiedName("LotteryNumberList", ""); // <xs:length value="5"/> XmlSchemaLengthFacet length = new XmlSchemaLengthFacet(); length.Value = "5"; LotteryNumbersRestriction.Facets.Add(length); LotteryNumbersType.Content = LotteryNumbersRestriction; schema.Items.Add(LotteryNumbersType); // <xs:element name="TodaysLottery" type="LotteryNumbers"> XmlSchemaElement TodaysLottery = new XmlSchemaElement(); TodaysLottery.Name = "TodaysLottery"; TodaysLottery.SchemaTypeName = new XmlQualifiedName("LotteryNumbers", ""); schema.Items.Add(TodaysLottery); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); schemaSet.Add(schema); schemaSet.Compile(); XmlSchema compiledSchema = null; foreach (XmlSchema schema1 in schemaSet.Schemas()) { compiledSchema = schema1; } XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); compiledSchema.Write(Console.Out, nsmgr); } public static void ValidationCallbackOne(object sender, ValidationEventArgs args) { Console.WriteLine(args.Message); } }
The following XML file is generated for the preceding code example.
<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="LotteryNumber">
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="99"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="LotteryNumberList">
<xs:list itemType="LotteryNumber"/>
</xs:simpleType>
<xs:simpleType name="LotteryNumbers">
<xs:restriction base="LotteryNumberList">
<xs:length value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="TodaysLottery" type="LotteryNumbers">
</xs:element>
</xs:schema>
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaType
System.Xml.Schema.XmlSchemaSimpleType
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaType
System.Xml.Schema.XmlSchemaSimpleType
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: