Home > Backend Development > C#.Net Tutorial > Detailed explanation of the sample code for reading XML multi-level child nodes in c#

Detailed explanation of the sample code for reading XML multi-level child nodes in c#

黄舟
Release: 2017-03-24 11:15:28
Original
2361 people have browsed it

本文主要介绍了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);
Copy after login

The above is the detailed content of Detailed explanation of the sample code for reading XML multi-level child nodes in c#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template