Create XML Schema : Schema « XML « ASP.Net

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
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.