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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » XML » XmlSchema 
30.20.1.Create Xml schema
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.