Mega Code Archive

 
Categories / ASP.Net / XML
 

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>