話不多說,請看代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
string xmlFilePath = "D:\\log_xml\\MarInfo.xml" ; //Server.MapPath(@"相對路徑如/xml/test.xml"); XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加載XML文件 string rst = "" ; //使用xpath表達式選擇文檔中所有的student子節點 XmlNodeList studentNodeList = doc.SelectNodes( "Root/MarketList/Market" ); if (studentNodeList != null ) { foreach (XmlNode studentNode in studentNodeList) { //通過Attributes獲得屬性名字為name的屬性 string name = studentNode.Attributes[ "MarketName" ].Value+ ":" ; rst+= name; //通過SelectSingleNode方法獲得當前節點下的SubMarketList子節點 XmlNode coursesNode = studentNode.SelectSingleNode( "SubMarketList" ); //通過ChildNodes屬性獲得courseNode的所有一級子節點 XmlNodeList courseNodeList = coursesNode.ChildNodes; if (courseNodeList != null ) { foreach (XmlNode courseNode in courseNodeList) { rst += courseNode.Attributes[ "Name" ].Value+ "," ; } rst += "<br/>" ; } } } Response.Write(rst); |
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/glwb/p/6606125.html