c#讀取XML多級子節點


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM