XmlSchema in the past has always troubled me in so many areas. So I decided to dig into this namespace trying to understand a little more on how to use it in real life.
I also wrote brief explanation to make life easier for understanding the code. However, I suggest that you should read more about the System.Xml.Schema namespace which I believe is very rich and powerful.
I hope that the sample code posted in here is at least beneficially to someone other than just me.
Try it out and let me know what you think
Books.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/books" xmlns:bk="http://www.example.com/books" attributeFormDefault="unqualified" elementFormDefault="qualified">
<xs:element name="books"> <xs:complexType> <xs:sequence> <xs:element name="book" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="author" type="xs:string" /> </xs:sequence> <xs:attribute name="publisher" type="bk:publisherType" use="required" /> <xs:attribute name="on-loan" type="xs:string" use="optional" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element>
<xs:annotation> <xs:documentation xml:lang="en"> The publisherType is a list of the publishers I've bought books from. If a publisher is not on the list then it means I don't have any books from them. </xs:documentation> </xs:annotation>
<xs:simpleType name="publisherType"> <xs:restriction base="xs:string"> <xs:enumeration value="WROX" /> <xs:enumeration value="Prentice Hall" /> <xs:enumeration value="Addison-Wesley" /> <xs:enumeration value="APress" /> <xs:enumeration value="IDG books" /> </xs:restriction> </xs:simpleType></xs:schema>
Schema.cs
using System;using System.Xml;using System.Xml.Schema;using System.Xml.Serialization;
namespace Test{ public class TestXmlSchema { public static void GetPublisherType() { // Load the xml schema definition into the xml schema Books object XmlSchema Books = XmlSchema.Read(new XmlTextReader("Books.xsd"), null); // Compile it with no validation Books.Compile(null); // Get the xml schema simpleType with the name "publisherType" and // the targetNamespace ""http://www.example.com/books" XmlSchemaSimpleType PubType = (XmlSchemaSimpleType) Books.SchemaTypes[new XmlQualifiedName("publisherType", "http://www.example.com/books")]; // Write out the parse value Console.WriteLine(PubType.Datatype.ParseValue("WROX", new NameTable(), null)); try { // Throws exception because "Microsoft Press" doesn't exist in the publisherType PubType.Datatype.ParseValue("Microsoft Press", new NameTable(), null); } catch(XmlSchemaException ex) { Console.WriteLine("XML Schema exception: " + ex.Message); Console.WriteLine("--------------------"); Console.WriteLine(); } } public static void DisplayXmlSchema() { XmlSchema items = XmlSchema.Read(new XmlTextReader("Books.xsd"), null); // Oh well, you got to read more about this guy items.Namespaces = new XmlSerializerNamespaces(); // Replace the Xml schema namespace xd with xsd // This is only done in memory, not permanent items.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema"); // Write out the content of Books.xsd to the console items.Write(Console.Out); Console.WriteLine(); Console.WriteLine("-------------------"); } public static void DisplayComment() { XmlSchema Books = XmlSchema.Read(new XmlTextReader("books.xsd"), null); Books.Compile(null); // Loop through the entire SchemaCollectionObject // Create an empty schema cso foreach(XmlSchemaObject xso in Books.Items) { XmlSchemaAnnotation xsa = xso as XmlSchemaAnnotation; // if the <xs:annotation> is found if(xsa != null) { // Get the content of the element <xs:documentation xml:lang="en"> string comment = ((XmlSchemaDocumentation)xsa.Items[0]).Markup[0].InnerText; // Write out the comment Console.WriteLine(comment); } } } } public class Class1 { public static void Main(string[] args) { TestXmlSchema.GetPublisherType(); TestXmlSchema.DisplayXmlSchema(); TestXmlSchema.DisplayComment(); } }}