validating-xml.aspx
来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 128 行
ASPX
128 行
<%@Page Language="C#"%>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.Schema" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Validating XML documents with an XmlValidatingReader object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Validating XML documents with an XmlValidatingReader object</span><hr />
<!--------------------------------------------------------------------------->
<form runat="server">
Select XML source document:
<ASP:DropDownList id="selXMLFile" runat="server">
<ASP:ListItem Text="A valid and well-formed XML document" Value="good-booklist.xml" />
<ASP:ListItem Text="A well-formed but invalid XML document" Value="bad-booklist.xml" />
<ASP:ListItem Text="An XML document that is invaild and not well-formed" Value="broken-booklist.xml" />
<ASP:ListItem Text="An XML document that doesn't even exist" Value="not-there.xml" />
</ASP:DropDownList>
<input type="submit" value="Go" /><p />
</form>
<div id="outXMLDoc" runat="server" /><p />
<script language="C#" runat="server">
// declare a variable to hold an XmlTextReader and one to hold the
// number of errors found. They have to be global because we need
// to access them within the event handler as well as Page_Load
XmlTextReader objXTReader ;
int intValidErrors = 0;
void Page_Load(Object sender, EventArgs e)
{
// create physical path to booklist sample files (in same folder as ASPX page)
string strCurrentPath = Request.PhysicalPath;
string strXMLPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\" + selXMLFile.SelectedItem.Value;
string strSchemaPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\booklist-schema.xsd";
// create the new XmlTextReader object and load the XML document
objXTReader = new XmlTextReader(strXMLPath);
outXMLDoc.InnerHtml = "Loaded file: <b><a href='" + strXMLPath
+ "'>" + strXMLPath + "</a></b><br />";
// create an XMLValidatingReader for this XmlTextReader
XmlValidatingReader objValidator = new XmlValidatingReader(objXTReader);
// set the validation type to use an XSD schema
objValidator.ValidationType = ValidationType.Schema;
// create a new XmlSchemaCollection
XmlSchemaCollection objSchemaCol = new XmlSchemaCollection();
// add the booklist-schema.xsd schema to it
objSchemaCol.Add("", strSchemaPath);
// assign the schema collection to the XmlValidatingReader
objValidator.Schemas.Add(objSchemaCol);
outXMLDoc.InnerHtml += "Validating against: <b><a href='" + strSchemaPath
+ "'>" + strSchemaPath + "</a></b><p />";
// add the event handler for any validation errors found
objValidator.ValidationEventHandler += new ValidationEventHandler(ValidationError);
try
{
// iterate through the document using the contents as required
// we simply read each element here without using it for anything
while (objValidator.Read())
{
// use or display the XML content here as required
}
// display count of errors found
outXMLDoc.InnerHtml += "<b>* Validation complete - "
+ intValidErrors + "</b> error(s) found";
}
catch (Exception objError)
{
// will occur if there is a read error or the document cannot be parsed
outXMLDoc.InnerHtml += "<b>* Read/Parser error:</b> " + objError.Message + "<br />";
}
finally
{
// must remember to always close the XmlTextReader after use
objXTReader.Close();
}
}
// ---------------------------------------------------------------------------
public void ValidationError(Object objSender, ValidationEventArgs objArgs)
{
// event handler called when a validation error is found
intValidErrors += 1; // increment count of errors
// check the severity of the error
string strSeverity = String.Empty;
switch (Convert.ToInt32(objArgs.Severity))
{
case 0: strSeverity = "Error"; break;
case 1: strSeverity = "Warning"; break;
}
// display a message
outXMLDoc.InnerHtml += "<b>* Validation error:</b> " + objArgs.Message
+ "<br /> Severity level: '<b>" + strSeverity + "</b>'. ";
if (objXTReader.LineNumber > 0)
outXMLDoc.InnerHtml += "Line: " + objXTReader.LineNumber
+ ", character: " + objXTReader.LinePosition + "<br />";
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?