話不多說,請看代碼:
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);
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網!
新聞熱點
疑難解答