c reads XML multilevel child nodes

  • 2021-12-12 05:24:09
  • OfStack

Without saying much, please look at the code:


string xmlFilePath = "D:\\log_xml\\MarInfo.xml"; //Server.MapPath(@" Relative paths such as /xml/test.xml");
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);// Loading XML Documents 
string rst = "";
// Use xpath Expression selects all the student Child node 
XmlNodeList studentNodeList = doc.SelectNodes("Root/MarketList/Market");
if (studentNodeList != null)
{
foreach (XmlNode studentNode in studentNodeList)
{
// Pass Attributes Get the property name is name Properties of 
string name = studentNode.Attributes["MarketName"].Value+":";
rst+= name;
// Pass SelectSingleNode Method under the current node to get the SubMarketList Child node 
XmlNode coursesNode = studentNode.SelectSingleNode("SubMarketList");
// Pass ChildNodes Attribute get courseNode All of 1 Level child node 
XmlNodeList courseNodeList = coursesNode.ChildNodes;
if (courseNodeList != null)
{
foreach (XmlNode courseNode in courseNodeList)
{
rst += courseNode.Attributes["Name"].Value+",";
}
rst += "<br/>";
}
}
}
Response.Write(rst);

Related articles: