使用Asp.Net中的XmlValidatingReader来验证XML Schema.
2024-07-10 12:57:55
供稿:网友
网站运营seo文章大全提供全面的站长运营经验及seo技术!这段时间,我正在学习xml,书上介绍使用xerces-c或xerces-j来验证xml文档的schema或dtd,结果,发现上面的工具不好用.
后来,只有放下书,到书店看看,有没有其他什么书能带来帮助.当检到professiona asp.net xml with c# 这本书时,里面讲到了使用system.xml 和 system.xml.schema命名空间下的xmltextreader类和xmlvalidatingreader类来验证带有schema或dtd的xml文档.便买下来了.
回寝室后,根据书里面面的介绍,写了一个小的测试程序,发现还很管用,在这里,但贴出来,希望给需要的人有帮助.
代码如下:
<%@page debug="true"%>
<%@import namespace="system"%>
<%@import namespace="system.xml"%>
<%@import namespace="system.xml.schema"%>
<script language="c#" runat="server">
protected void uploadbtn_click(object sender,eventargs e)
{
xmltextreader myxml = null;
xmlvalidatingreader myxmldoc = null;
int nelements = 0; //统计文档中element的个数
int nattributes = 0; //统计文档中attributes的个数
int ncomments = 0; //统计文档中 comment的个数
int npis = 0; //统计文档中processinginstruction的个数
int ncdatas = 0; //统计文档中cdata的个数
string sworkingtext = "";
resultarea.text = "";
try
{
//装载xml document
myxml = new xmltextreader(fileselector.postedfile.inputstream);
myxml.whitespacehandling = whitespacehandling.none;
//xmlvalidatingreader是xmltextreader的帮助功能
//需要从xmltextreader对象获取实例
myxmldoc = new xmlvalidatingreader(myxml);
myxmldoc.validationtype = validationtype.auto;
//验证事件委托的签名
myxmldoc.validationeventhandler += new validationeventhandler(this.validationeventcallback);
//在遍历xml文档的时候,xmlvalidatingreadert将根据schema或dtd
//对xml进行验证。
while(myxmldoc.read())
{
//这里面的语句的作用主要是分析xml文档各node,
//但while循环还是必要,因为只有逐个遍历所有个的node,才会检查出验证是否匹配。
switch(myxmldoc.nodetype) //judge crrent node's type. 判定当前结点的类型
{
case xmlnodetype.element : // element 元素
++nelements;
nattributes += myxmldoc.attributecount;
break;
case xmlnodetype.text : //文本内容
sworkingtext ="<b>text:</b>" + myxmldoc.value + "<br/>";
resultarea.text += sworkingtext;
break;
case xmlnodetype.comment: //注释
++ncommen;
break;
case xmlnodetype.processinginstruction: //处理指令
++npis;
break;
case xmlnodetype.cdata: //cdata
++ncdatas;
break;
}
}
sworkingtext = "<br/>" + "total number of elements:" + nelements + "<br/>";
sworkingtext += "total number of attributes in document:" + nattributes + "<br/>";
sworkingtext += "total comments:" + ncomments + "<br/>";
sworkingtext += "total processinginstructions:" + npis + "<br/>";
sworkingtext += "total cdata sections:" + ncdatas;
resultarea.text += sworkingtext;
}
catch(xmlexception exec)
{
sworkingtext = "exception while parsing:" + "<br/>";
sworkingtext += "line number:" + exec.linenumber + "<br/>";
sworkingtext += "message:" + exec.message + "<br/>" + "<br/>";
sworkingtext += "line position:" + exec.lineposition + "<br/>";
sworkingtext += "stack trace:" + "<br/>" + exec.stacktrace;
resultarea.text = sworkingtext;
}
finally
{
if (myxmldoc!=null)
{
myxmldoc.close();
}
}
}
//验证事件
public void validationeventcallback(object sender,validationeventargs e)
{
if (e.severity == xmlseveritytype.warning)//区分是警告还是错误
{
resultarea.text += "<b>validation warning: </b>" + e.message + "<br/>";
}
else
{
resultarea.text += "<b>validation errors: </b>" + e.message + "<br/>";
}
}
</script>
<html>
<head><title>xmltextreader</title></head>
<body>
<form method="post" runat="server">
<asp:label font-size="10pt" runat="server">上传xml文档 在服务器验证</asp:label><br/>
<input type="file" size="40" runat="server" id="fileselector"> <br/>
<asp:button id="uploadbtn" text="提交" onclick="uploadbtn_click" runat="server"/><br/>
<asp:label font-size="12pt" id="resultarea" runat="server"/><br/>
</form>
</body>
</html>