参考文章:
一共有四种方法,创建一个asp.net项目,下面有四个页面,如下图所示:
data7.xml内容如下所示:
<?xml version="1.0" encoding="utf-8"?> <Advertisements> <Ad> <ImageUrl>001.jpg</ImageUrl> <NavigateUrl>001.aspx</NavigateUrl> <Impressions>10</Impressions> <Keyword>gucas01</Keyword> </Ad> <Ad> <ImageUrl>002.jpg</ImageUrl> <NavigateUrl>002.aspx</NavigateUrl> <Impressions>20</Impressions> <Keyword>gucas02</Keyword> </Ad> <Ad> <ImageUrl>003.jpg</ImageUrl> <NavigateUrl>003.aspx</NavigateUrl> <Impressions>30</Impressions> <Keyword>gucas03</Keyword> </Ad> <Ad> <ImageUrl>004.jpg</ImageUrl> <NavigateUrl>004.aspx</NavigateUrl> <Impressions>40</Impressions> <Keyword>gucas04</Keyword> </Ad> <Ad> <ImageUrl>005.jpg</ImageUrl> <NavigateUrl>005.aspx</NavigateUrl> <Impressions>50</Impressions> <Keyword>gucas05</Keyword> </Ad> <Ad> <ImageUrl>google.gif</ImageUrl> <NavigateUrl>http://www.google.com</NavigateUrl> <Impressions>30</Impressions> <Keyword>google</Keyword> </Ad> </Advertisements>
方法1:使用XML控件
Default.aspx代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <h3><font face="Verdana">读取XML方法一:使用XML控件</font></h3> <form id="form1" runat="server"> <div> <asp:Xml id="xml1" DocumentSource="data7.xml" runat="server" /> </div> </form> </body> </html>
Default.aspx.cs不做修改。
发布结果如下图所示:
方法二: 使用DOM技术
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <h3><font face="Verdana">读取XML方法二:使用DOM技术</font></h3> <form id="form1" runat="server"> <div> <asp:Xml id="xml1" runat="server" /> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("data7.xml")); xml1.Document = doc; } }
发布结果如下所示:
方法三 :使用DataSet对象
Default.aspx
<body> <h3><font face="Verdana">读取XML方法三:使用DataSet对象</font></h3> <form id="form1" runat="server"> <div> <asp:DataGrid id="dgEmployees" runat="server" /> </div> </form> </body>
Default.aspx.cs
public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataSet objDataSet = new DataSet(); objDataSet.ReadXml(Server.MapPath("data7.xml")); //方法1 //dgEmployees.DataSource = objDataSet; //dgEmployees.DataBind(); //方法2 dgEmployees.DataSource = objDataSet.Tables["Ad"].DefaultView; dgEmployees.DataBind(); } }
发布结果如下所示:
方法四 :按文本方式读取
Default.aspx
<body> <h3><font face="Verdana">读取XML方法四:按文本方式读取</font></h3> <form id="form1" runat="server"> <div> <asp:label id="LblFile" runat="server" /> </div> </form> </body>
Default.aspx.cs
public partial class Default4 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { XmlTextReader objXMLReader = new XmlTextReader(Server.MapPath("web.config")); string strNodeResult = ""; XmlNodeType objNodeType; while (objXMLReader.Read()) { objNodeType = objXMLReader.NodeType; switch (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 += " -Value:<b>" + objXMLReader.Value + "</b><br/>"; break; } if (objXMLReader.HasAttributes) { //用循环判断完所有节点 while (objXMLReader.MoveToNextAttribute()) { //取标签和值 strNodeResult += " -Attribute:<b>" + objXMLReader.Name + "</b> value:<b>" + objXMLReader.Value + "</b><br/>"; } } LblFile.Text = strNodeResult; } } }
发布结果如下所示: