在asp.net中读取XML文件信息的4种方法
方法一 :使用XML控件
1
<% @ Page Language="C#"%>
2
<html>
3
<body>
4
<h3><font face="Verdana">读取XML方法一</font></h3>
5
<from runat=server>
6
<asp:Xml id="xml1" DocumentSource="grade.xml" runat="server" />
7
</from>
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
</body>
10
</html>
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
方法二: 使用DOM技术
1 <% @ Page Language="C#"%>
2 <% @ Import Namespace="System.Xml"%>
3 <% @ Import Namespace="System.Xml.Xsl"%>
4 <html>
5 <script language="C#" runat="server">
6 void Page_Load(Object sender,EventArgs e)
7 {
8 XmlDocument doc=new XmlDocument();
9 doc.Load(Server.MapPath("grade.xml"));
10 xml1.Document=doc;
11 }
12 </script>
13 <body>
14 <h3><font face="Verdana">读取XML方法二</font></h3>
15 <from runat=server>
16 <asp:Xml id="xml1" runat="server" />
17 </from>
18
19 </body>
20 </html>
21
2 <% @ Import Namespace="System.Xml"%>
3 <% @ Import Namespace="System.Xml.Xsl"%>
4 <html>
5 <script language="C#" runat="server">
6 void Page_Load(Object sender,EventArgs e)
7 {
8 XmlDocument doc=new XmlDocument();
9 doc.Load(Server.MapPath("grade.xml"));
10 xml1.Document=doc;
11 }
12 </script>
13 <body>
14 <h3><font face="Verdana">读取XML方法二</font></h3>
15 <from runat=server>
16 <asp:Xml id="xml1" runat="server" />
17 </from>
18
19 </body>
20 </html>
21
方法三 :使用DataSet对象
1 <% @ Page Language="C#"%>
2 <% @ Import Namespace="System.Data"%>
3 <% @ Import Namespace="System.Data.OleDb"%>
4 <script language="C#" runat="server">
5 void Page_Load(Object sender,EventArgs e)
6 {
7 DataSet objDataSet=new DataSet();
8 objDataSet.ReadXml(Server.MapPath("grade.xml"));
9 dgEmployees.DataSource=objDataSet.Tables["student"].DefaultView;
10 dgEmployees.DataBind();
11 }
12 </script>
13 <body>
14 <h3><font face="Verdana">读取XML方法三</font></h3>
15 <asp:DataGrid id="dgEmployees" runat="server" />
16
17 </body>
18 </html>
19
2 <% @ Import Namespace="System.Data"%>
3 <% @ Import Namespace="System.Data.OleDb"%>
4 <script language="C#" runat="server">
5 void Page_Load(Object sender,EventArgs e)
6 {
7 DataSet objDataSet=new DataSet();
8 objDataSet.ReadXml(Server.MapPath("grade.xml"));
9 dgEmployees.DataSource=objDataSet.Tables["student"].DefaultView;
10 dgEmployees.DataBind();
11 }
12 </script>
13 <body>
14 <h3><font face="Verdana">读取XML方法三</font></h3>
15 <asp:DataGrid id="dgEmployees" runat="server" />
16
17 </body>
18 </html>
19
方法四 :按文本方式读取
1 <% @ Page Language="C#"%>
2 <% @ Import Namespace="System.Xml"%>
3 <html>
4 <script language="C#" runat="server">
5 private void Page_Load(Object sender,EventArgs e)
6 {
7 XmlTextReader objXMLReader=new XmlTextReader(Server.MapPath("grade.xml"));
8 string strNodeResult="";
9 XmlNodeType objNodeType;
10 while(objXMLReader.Read())
11 {
12 objNodeType =objXMLReader.NodeType;
13 swith(objNodeType)
14 {
15 case XmlNodeType.XmlDeclaration:
16 //读取XML文件头
17 strNodeResult+="XML Declaration:<b>"+objXMLReader.Name+""+objXMLReader.Value+"</b><br/>";
18 break;
19 case XmlNodeType.Element:
20 //读取标签
21 strNodeResult+="Element:<b>"+objXMLReader.Name+"</b><br/>";
22 break;
23 case XmlNodeType.Text:
24 //读取值
25 strNodeResult+=" -Value:<b>"+objXMLReader.Value+"</b><br/>";
26 break;
27
28 }
29 //判断该节点是否有属性
30 if(objXMLReader.AttributeCount>0)
31 { //用循环判断完所有节点
32 while(objXMLReader.MoveToNextAttibute)
33 { //取标签和值
34 strNodeResult+=" -Attribute:<b>"+objXMLReader.Name+"</b> value:<b>"+objXMLReader.Value+"</b><br/>";
35 }
36 }
37 LblFile.Text=strNodeResult;
38 }
39 }
40 </script>
41 <body>
42 <h3><font face="Verdana">读取XML方法四</font></h3>
43 <from runat=server>
44 <asp:label id="LblFile" runat="server" />
45 </from>
46
47 </body>
48 </html>
49
2 <% @ Import Namespace="System.Xml"%>
3 <html>
4 <script language="C#" runat="server">
5 private void Page_Load(Object sender,EventArgs e)
6 {
7 XmlTextReader objXMLReader=new XmlTextReader(Server.MapPath("grade.xml"));
8 string strNodeResult="";
9 XmlNodeType objNodeType;
10 while(objXMLReader.Read())
11 {
12 objNodeType =objXMLReader.NodeType;
13 swith(objNodeType)
14 {
15 case XmlNodeType.XmlDeclaration:
16 //读取XML文件头
17 strNodeResult+="XML Declaration:<b>"+objXMLReader.Name+""+objXMLReader.Value+"</b><br/>";
18 break;
19 case XmlNodeType.Element:
20 //读取标签
21 strNodeResult+="Element:<b>"+objXMLReader.Name+"</b><br/>";
22 break;
23 case XmlNodeType.Text:
24 //读取值
25 strNodeResult+=" -Value:<b>"+objXMLReader.Value+"</b><br/>";
26 break;
27
28 }
29 //判断该节点是否有属性
30 if(objXMLReader.AttributeCount>0)
31 { //用循环判断完所有节点
32 while(objXMLReader.MoveToNextAttibute)
33 { //取标签和值
34 strNodeResult+=" -Attribute:<b>"+objXMLReader.Name+"</b> value:<b>"+objXMLReader.Value+"</b><br/>";
35 }
36 }
37 LblFile.Text=strNodeResult;
38 }
39 }
40 </script>
41 <body>
42 <h3><font face="Verdana">读取XML方法四</font></h3>
43 <from runat=server>
44 <asp:label id="LblFile" runat="server" />
45 </from>
46
47 </body>
48 </html>
49