c#读取XML多级子节点

作者:袖梨 2022-06-25
 
 代码如下 复制代码
stringxmlFilePath ="D:\log_xml\MarInfo.xml";//Server.MapPath(@"相对路径如/xml/test.xml");
XmlDocument doc =newXmlDocument();
doc.Load(xmlFilePath);//加载XML文件
stringrst ="";
//使用xpath表达式选择文档中所有的student子节点
XmlNodeList studentNodeList = doc.SelectNodes("Root/MarketList/Market");
if(studentNodeList !=null)
{
foreach(XmlNode studentNodeinstudentNodeList)
{
//通过Attributes获得属性名字为name的属性
stringname = studentNode.Attributes["MarketName"].Value+":";
rst+= name;
//通过SelectSingleNode方法获得当前节点下的SubMarketList子节点
XmlNode coursesNode = studentNode.SelectSingleNode("SubMarketList");
//通过ChildNodes属性获得courseNode的所有一级子节点
XmlNodeList courseNodeList = coursesNode.ChildNodes;
if(courseNodeList !=null)
{
foreach(XmlNode courseNodeincourseNodeList)
{
rst += courseNode.Attributes["Name"].Value+",";
}
rst +="
";
}
}
}
Response.Write(rst);
 

相关文章

精彩推荐