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);