<%@ Page Language="C#"%>
<%@ Import Namespace="System. IO" %>
<%@ Import Namespace="System. Xml" %>
<%@ Import Namespace="System. Xml.Schema" %>
<script runat="server">
private StringBuilder stringBuilder = new StringBuil der();
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 XmlSchemaElemen t();
authorID.Name = " authorID";
authorID. SchemaTypeName = new XmlQualif iedName("string", ns );
XmlSchemaElement authorLastName = new XmlSchema Element();
authorLastName.Name = "lastName";
authorLastName. SchemaTypeName = new XmlQualif iedName("string", ns);
XmlSchemaElement authorFirstName = new XmlSchem aElement();
authorFirstName.Name = "firstName";
authorFirstName. SchemaTypeName = new XmlQualif iedName("string", ns);
XmlSchemaElement zip = new XmlSchemaElement();
zip.Name = "zip";
zip.SchemaTypeName = n ew XmlQualifiedName(" unsignedInt", ns);
XmlSchemaElement contract = new XmlSchemaElemen t();
contract.Name = " contract";
contract. SchemaTypeName = new XmlQualif iedName("boolean", ns);
XmlSchemaElement authorElement = new XmlSchemaE lement();
authorElement.Name = " author";
// Create an anonymous complex type for the author element
XmlSchemaComplexType authorType = new XmlSchemaComp lexType();
XmlSchemaSequence authorSeq = new XmlSchemaSeque nce();
//Add all the child elements to the sequence
authorSeq.Items.Add(au thorID);
authorSeq.Items.Add(au thorLastName);
authorSeq.Items.Add(au thorFirstName);
authorSeq.Items.Add(zi p);
authorSeq.Items.Add(co ntract);
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(autho rElement);
//Compile the file to check for validation errors
schema.Compile(new Val idationEventHandler(Validation EventHandler));
FileStream stream = ne w 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 ValidationEventHandle r(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>
<%@ Import Namespace="System.
<%@ Import Namespace="System.
<%@ Import Namespace="System.
<script runat="server">
private StringBuilder
void Page_Load(object
{
string ns = "http://
string xsdPath =
XmlSchema schema = new
XmlSchemaElement
authorID.Name = "
authorID.
XmlSchemaElement
authorLastName.Name =
authorLastName.
XmlSchemaElement
authorFirstName.Name =
authorFirstName.
XmlSchemaElement zip =
zip.Name = "zip";
zip.SchemaTypeName = n
XmlSchemaElement
contract.Name = "
contract.
XmlSchemaElement
authorElement.Name = "
// Create an
XmlSchemaComplexType
XmlSchemaSequence
//Add all the child
authorSeq.Items.Add(au
authorSeq.Items.Add(au
authorSeq.Items.Add(au
authorSeq.Items.Add(zi
authorSeq.Items.Add(co
authorType.Particle =
//Set the SchemaType
authorElement.
//Add the root
schema.Items.Add(autho
//Compile the file to
schema.Compile(new Val
FileStream stream = ne
//Write the file
schema.Write(stream);
stream.Close();
if (stringBuilder.
Response.Write("
else
Response.Write("
}
void ValidationEventHandle
{
stringBuilder.Append("
}
</script>
<html xmlns="http://www.w3.
<head runat="server">
<title>Writing XSD Schema<
</head>
<body>
<form id="form1" runat="
<div>
</div>
</form>
</body>
</html>
No comments :
Post a Comment