方法一:使用XML控件
<%@PageLanguage="C#"%>
<html>
<body>
<h3><fontface="Verdana">读取XML方法一</font></h3>
<fromrunat=server>
<asp:Xmlid="xml1"DocumentSource="grade.xml"runat="server"/>
</from>
</body>
</html>
方法二:使用DOM技术
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Xml"%>
<%@ImportNamespace="System.Xml.Xsl"%>
<html>
<scriptlanguage="C#"runat="server">
voidPage_Load(Objectsender,EventArgse)
{
XmlDocumentdoc=newXmlDocument();
doc.Load(Server.MapPath("grade.xml"));
xml1.Document=doc;
}
</script>
<body>
<h3><fontface="Verdana">读取XML方法二</font></h3>
<fromrunat=server>
<asp:Xmlid="xml1"runat="server"/>
</from>
</body>
</html>
方法三:使用DataSet对象
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Data"%>
<%@ImportNamespace="System.Data.OleDb"%>
<scriptlanguage="C#"runat="server">
voidPage_Load(Objectsender,EventArgse)
{
DataSetobjDataSet=newDataSet();
objDataSet.ReadXml(Server.MapPath("grade.xml"));
dgEmployees.DataSource=objDataSet.Tables["student"].DefaultView;
dgEmployees.DataBind();
}
</script>
<body>
<h3><fontface="Verdana">读取XML方法三</font></h3>
<asp:DataGridid="dgEmployees"runat="server"/>
</body>
</html>
方法四:按文本方式读取
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Xml"%>
<html>
<scriptlanguage="C#"runat="server">
privatevoidPage_Load(Objectsender,EventArgse)
{
XmlTextReaderobjXMLReader=newXmlTextReader(Server.MapPath("grade.xml"));
stringstrNodeResult="";
XmlNodeTypeobjNodeType;
while(objXMLReader.Read())
{
objNodeType=objXMLReader.NodeType;
swith(objNodeType)
{
caseXmlNodeType.XmlDeclaration:
//读取XML文件头
strNodeResult+="XMLDeclaration:<b>"+objXMLReader.Name+""+objXMLReader.Value+"</b><br/>";
break;
caseXmlNodeType.Element:
//读取标签
strNodeResult+="Element:<b>"+objXMLReader.Name+"</b><br/>";
break;
caseXmlNodeType.Text:
//读取值
strNodeResult+="-Value:<b>"+objXMLReader.Value+"</b><br/>";
break;
}
//判断该节点是否有属性
if(objXMLReader.AttributeCount>0)
{//用循环判断完所有节点
while(objXMLReader.MoveToNextAttibute)
{//取标签和值
strNodeResult+="-Attribute:<b>"+objXMLReader.Name+"</b>value:<b>"+objXMLReader.Value+"</b><br/>";
}
}
LblFile.Text=strNodeResult;
}
}
</script>
<body>
<h3><fontface="Verdana">读取XML方法四</font></h3>
<fromrunat=server>
<asp:labelid="LblFile"runat="server"/>
</from>
</body>
</html>
【ASP.NET中读取XML文件信息的4种方法与示例代码】相关文章:
★ ASP.NET 2.0下随机读取Access记录的实现方法
★ ASP.NET Dictionary 的基本用法示例介绍