Create XML Schema : Schema « XML « ASP.Net

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
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
w_w__w_._ja___v_a__2__s_._c_o_m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.