Create XML Schema : Schema « XML « ASP.Net

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 = "";
        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
        authorType.Particle = authorSeq;     
        //Set the SchemaType of authors element to the complex type
        authorElement.SchemaType = authorType;               
        //Add the root authors element to the schema
        //Compile the file to check for validation errors
        schema.Compile(new ValidationEventHandler(ValidationEventHandler));                    
        FileStream stream = new FileStream(xsdPath, FileMode.Create);
        //Write the file
        if (stringBuilder.ToString() == String.Empty)
            Response.Write("File written successfully");
            Response.Write("Schema Creation Failed. <br>" + stringBuilder.ToString());        

    void ValidationEventHandler(object sender, ValidationEventArgs args)
        stringBuilder.Append("Validation error: " + args.Message + "<br>");                
<html xmlns="" >
<head runat="server">
    <title>Writing XSD Schema</title>
    <form id="form1" runat="server">


Related examples in the same category

1.Validate XML with inline Schema
2.Read XML Schema and compile