XML data validation
<%@ Page Language="C#"%>
<%@ 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 xmlPath = MapPath("Authors.xml");
string xsdPath = MapPath("Authors.xsd");
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(xsdPath));
XmlReader reader = XmlReader.Create(xmlPath, settings);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
doc.Validate(eventHandler);
if (stringBuilder.ToString() == String.Empty)
Response.Write("Validation completed successfully.");
else
Response.Write("Validation 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>XSD Validation</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Related examples in the same category