分析ASP.NET读取XML文件4种方法

2013 年 5 月 6 日3750

<% @ Page Language="C#"%>

<% @ Import Namespace="System.Xml"%>

<html>

<script language="C#" runat="server">

private void Page_Load(Object sender,EventArgs e)

{

XmlTextReader objXMLReader=new XmlTextReader(Server.MapPath("grade.xml"));

string strNodeResult="";

XmlNodeType objNodeType;

while(objXMLReader.Read())

{

objNodeType =objXMLReader.NodeType;

swith(objNodeType)

{

case XmlNodeType.XmlDeclaration:

//读取XML文件头

strNodeResult+="XML Declaration:<b>"+objXMLReader.Name+""+objXMLReader.Value+"</b><br/>";

break;

case XmlNodeType.Element:

//读取标签

strNodeResult+="Element:<b>"+objXMLReader.Name+"</b><br/>";

break;

case XmlNodeType.Text:

//读取值

strNodeResult+="&nbsp;-Value:<b>"+objXMLReader.Value+"</b><br/>";

break;

}

//判断该节点是否有属性

if(objXMLReader.AttributeCount>0)

{ //用循环判断完所有节点

while(objXMLReader.MoveToNextAttibute)

{ //取标签和值

strNodeResult+="&nbsp;-Attribute:<b>"+objXMLReader.Name+"</b>&nbsp;value:<b>"+objXMLReader.Value+"</b><br/>";

}

}

LblFile.Text=strNodeResult;

}

}

</script>

<body>

<h3><font face="Verdana">读取XML方法四</font></h3>

<from runat=server>

<asp:label id="LblFile" runat="server" />

</from>

</body>

</html>

0 0