Create XML Schema : Schema « XML « ASP.Net

Home
ASP.Net
1.ADO.net Database
2.Ajax
3.Asp Control
4.Collections
5.Components
6.Data Binding
7.Development
8.File Directory
9.HTML Control
10.Language Basics
11.Login Security
12.Mobile Control
13.Network
14.Page
15.Request
16.Response
17.Server
18.Session Cookie
19.Sitemap
20.Theme Style
21.User Control and Master Page
22.Validation by Control
23.Validation by Function
24.WebPart
25.WPF
26.XML
ASP.Net » XML » Schema 
Create XML Schema

<%@ Page Language="C#"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Schema" %>

<script runat="server">    
    private StringBuilder stringBuilder = new StringBuilder();
    void Page_Load(object sender, EventArgs e)
    {        
        string ns = "http://www.w3.org/2001/XMLSchema";
        string xsdPath = MapPath("NewAuthors.xsd");        
        XmlSchema schema = new XmlSchema();                     
        
        XmlSchemaElement authorID = new XmlSchemaElement();
        authorID.Name = "authorID";
        authorID.SchemaTypeName = new XmlQualifiedName("string", ns );
        
        XmlSchemaElement authorLastName = new XmlSchemaElement();
        authorLastName.Name = "lastName";
        authorLastName.SchemaTypeName = new XmlQualifiedName("string", ns);
        
        XmlSchemaElement authorFirstName = new XmlSchemaElement();
        authorFirstName.Name = "firstName";
        authorFirstName.SchemaTypeName = new XmlQualifiedName("string", ns);
        
        XmlSchemaElement zip = new XmlSchemaElement();
        zip.Name = "zip";
        zip.SchemaTypeName = new XmlQualifiedName("unsignedInt", ns);
        
        XmlSchemaElement contract = new XmlSchemaElement();
        contract.Name = "contract";
        contract.SchemaTypeName = new XmlQualifiedName("boolean", ns);       
              
        XmlSchemaElement authorElement = new XmlSchemaElement();
        authorElement.Name = "author";        
                
        // Create an anonymous complex type for the author element
        XmlSchemaComplexType authorType = new XmlSchemaComplexType();
        XmlSchemaSequence authorSeq = new XmlSchemaSequence();
        //Add all the child elements to the sequence
        authorSeq.Items.Add(authorID);
        authorSeq.Items.Add(authorLastName);
        authorSeq.Items.Add(authorFirstName);
        authorSeq.Items.Add(zip);
        authorSeq.Items.Add(contract);
        authorType.Particle = authorSeq;     
           
        //Set the SchemaType of authors element to the complex type
        authorElement.SchemaType = authorType;               
        //Add the root authors element to the schema
        schema.Items.Add(authorElement);
        //Compile the file to check for validation errors
        schema.Compile(new ValidationEventHandler(ValidationEventHandler));                    
        FileStream stream = new FileStream(xsdPath, FileMode.Create);
        //Write the file
        schema.Write(stream);
        stream.Close();
        if (stringBuilder.ToString() == String.Empty)
            Response.Write("File written successfully");
        else
            Response.Write("Schema Creation Failed. <br>" + stringBuilder.ToString());        
    }   

    void ValidationEventHandler(object sender, ValidationEventArgs args)
    {        
        stringBuilder.Append("Validation error: " + args.Message + "<br>");                
    }    
  
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Writing XSD Schema</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>                
    </div>
    </form>
</body>
</html>

           
       
Related examples in the same category
1.Validate XML with inline Schema
2.Read XML Schema and compile
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.