XmlSchema is an in-memory representation of an XML Schema
using System;
using System.Xml;
using System.Xml.Schema;
class XMLSchemaExamples
{
public static void Main()
{
XmlSchema schema = new XmlSchema();
XmlSchemaElement elementCat = new XmlSchemaElement();
schema.Items.Add(elementCat);
elementCat.Name = "cat";
elementCat.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
XmlSchemaElement elementPets = new XmlSchemaElement();
schema.Items.Add(elementPets);
elementPets.Name = "pets";
XmlSchemaComplexType complexType = new XmlSchemaComplexType();
elementPets.SchemaType = complexType;
XmlSchemaChoice choice = new XmlSchemaChoice();
complexType.Particle = choice;
choice.MinOccurs = 0;
choice.MaxOccursString = "unbounded";
XmlSchemaElement catRef = new XmlSchemaElement();
choice.Items.Add(catRef);
catRef.RefName = new XmlQualifiedName("cat");
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);
}
}
Related examples in the same category