using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace exercise_08
{
/// <summary>
/// _1 的摘要说明。
/// </summary>
public class _1 : Page
{
protected Label Label1;
protected TextBox txtFileName;
protected System.Web.UI.WebControls.Button Button1;
protected Button btnDisplay;
private void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
protected override void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnDisplay_Click(object sender, EventArgs e)
{
ValidateAndDisplayXmlDocument();
}
private void ValidateAndDisplayXmlDocument()
{
string filePath = Server.MapPath(txtFileName.Text.Trim()); //指定XML文件路径
XmlTextReader xr = new XmlTextReader(filePath);
XmlValidatingReader xv = new XmlValidatingReader(xr); //构造一个XmlValidatingReader
string outputStr = "<br><br><br><br><br>";
bool isEmpty = false;
xv.ValidationType = ValidationType.Auto;
try
{
while (xv.Read())
{
switch (xv.NodeType)
{
case XmlNodeType.Element: //如果当前节点是元素标记
if (xv.IsEmptyElement )
{
isEmpty = true;
}
else
{
isEmpty = false;
}
outputStr += indent(xv.Depth); //根据当前节点的深度输出不同的缩进
outputStr += "<font color=blue><" + xv.Name + "</font>"; //输出元素标记的起始部分 例如:<手机型号
while (xv.MoveToNextAttribute()) //在当前元素中遍历所有属性
{
outputStr += " <i>" + xv.Name + "=\"" + xv.Value + "\"</i> "; //输出属性,用斜体表示,例如 型号="6030"
}
if (isEmpty) //判断当前节点是否为空元素
{
outputStr += "<font color=blue>/></font><br>"; //若是,则以/>结尾
}
else
{
outputStr += "<font color=blue>></font><br>"; //若不是,则以>结尾
}
break;
case XmlNodeType.EndElement: //如果当前节点是元素结束标记
outputStr += indent(xv.Depth); //缩进
outputStr += "<font color=blue></" + xv.Name + "></font><br>"; //输出元素标记,如</手机型号>
break;
case XmlNodeType.Text: //如果当前节点是文本类型
if (xv.Value.Length != 0)
{
outputStr += indent(xv.Depth);
outputStr += "<font color=red>" + xv.Value + "</font><br>"; //用红色字体输出文本,如 诺基亚
}
break;
case XmlNodeType.Comment:
outputStr += "<font color=purple><!--" + xv.Value + "--></font><br>";
break;
case XmlNodeType.XmlDeclaration:
outputStr += "<?" + xv.Value + "?><br>";
break;
case XmlNodeType.CDATA:
outputStr += "<![CDATA" + xv.Value.Replace("\n", "<br>") + "]]>";
break;
}
}
Response.Write(outputStr);
}
catch (Exception err)
{
Response.Write("<br><br><br><br><br>" + "<font color=red>出现了以下错误,请检查:</font><br>" + err.ToString()); //若验证时发生错误,则输出异常
}
finally
{
xv.Close(); //关闭XmlValidatingReader
}
}
private string indent(int number)
{
string spaces = "";
for (int i = 0; i < number; i++)
{
spaces += " "; //输出8 * 当前节点深度 个空格
}
return spaces;
}
private void Button1_Click(object sender, System.EventArgs e)
{
DateTime aa = Convert.ToDateTime("2005-03-31");
Response.Write(aa.AddMonths(1));
}
}
}