这段时间,我正在学习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>
如果对实现功能有兴趣,可以翻阅相关的书或.Net Framework文档来熟悉XmlTextReader和XmlValidatingReader.
代码编写有不成熟的地方请多指教!谢谢!
本文地址:http://com.8s8s.com/it/it44834.htm