Create Xml schema : XmlSchema « XML « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

    public class MainClass
    {
        public static void Main()
        {
            XmlSchema schema = new XmlSchema();

            XmlSchemaSimpleType nametype = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeRestriction nameRes = new XmlSchemaSimpleTypeRestriction();
            nameRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            XmlSchemaMinLengthFacet nameFacet1 = new XmlSchemaMinLengthFacet();
            nameFacet1.Value = "3";
            XmlSchemaMaxLengthFacet nameFacet2 = new XmlSchemaMaxLengthFacet();
            nameFacet2.Value = "255";
            nameRes.Facets.Add(nameFacet1);
            nameRes.Facets.Add(nameFacet2);
            nametype.Content = nameRes;

            XmlSchemaSimpleType phonetype = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeRestriction phoneRes = new XmlSchemaSimpleTypeRestriction();
            phoneRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            XmlSchemaMaxLengthFacet phoneFacet1 = new XmlSchemaMaxLengthFacet();
            phoneFacet1.Value = "20";
            phoneRes.Facets.Add(phoneFacet1);
            phonetype.Content = phoneRes;

            XmlSchemaSimpleType notestype = new XmlSchemaSimpleType();
            XmlSchemaSimpleTypeRestriction notesRes = new XmlSchemaSimpleTypeRestriction();
            notesRes.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
            XmlSchemaMaxLengthFacet notesFacet1 = new XmlSchemaMaxLengthFacet();
            notesFacet1.Value = "500";
            notesRes.Facets.Add(notesFacet1);
            notestype.Content = notesRes;

            XmlSchemaComplexType employeetype = new XmlSchemaComplexType();
            XmlSchemaSequence sequence = new XmlSchemaSequence();
            XmlSchemaElement firstname = new XmlSchemaElement();
            firstname.Name = "firstname";
            firstname.SchemaType = nametype;
            XmlSchemaElement homephone = new XmlSchemaElement();
            homephone.Name = "homephone";
            homephone.SchemaType = phonetype;
            XmlSchemaElement notes = new XmlSchemaElement();
            notes.Name = "notes";
            notes.SchemaType = notestype;

            sequence.Items.Add(firstname);
            sequence.Items.Add(homephone);
            sequence.Items.Add(notes);

            employeetype.Particle = sequence;

            XmlSchemaAttribute employeeid = new XmlSchemaAttribute();
            employeeid.Name = "employeeid";
            employeeid.SchemaTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
            employeeid.Use = XmlSchemaUse.Required;

            employeetype.Attributes.Add(employeeid);

            XmlSchemaComplexType complextype = new XmlSchemaComplexType();
            XmlSchemaSequence sq = new XmlSchemaSequence();
            XmlSchemaElement employee = new XmlSchemaElement();
            employee.Name = "employee";
            employee.SchemaType = employeetype;
            employee.MinOccurs = 0;
            employee.MaxOccursString = "unbounded";
            sq.Items.Add(employee);
            complextype.Particle = sq;

            XmlSchemaElement employees = new XmlSchemaElement();
            employees.Name = "employees";
            employees.SchemaType = complextype;

            schema.Items.Add(employees);

            XmlSchemaSet set = new XmlSchemaSet();
            set.Add(schema);
            set.Compile();

            XmlTextWriter writer=new XmlTextWriter("yourvalue",null);
            schema.Write(writer);
            writer.Close();
        }
    }








30.20.XmlSchema
30.20.1.Create Xml schema
30.20.2.Register Schema validation event
30.20.3.Schema Validation