递归读取xml文件加载到TreeView上

XmlDocument方式递归读取xml文件

public static void ReadXMLRecursion()
{
    string xmlPath = AppDomain.CurrentDomain.BaseDirectory;
    xmlPath = xmlPath.Replace("bin\\Debug", "");
    string filePath = xmlPath + "xml\\Person.xml";
    //1,读取xml文件(XDocument)兼容2.0以上版本
    XDocument xDoc = XDocument.Load("../../xml/Person.xml");

    //2,获取根节点
    XElement rootElement = xDoc.Root;
    //3,将xml的根元素加载到TreeView的根节点上
    TreeNode rootNode=treeView.Nodes.Add(rootElement);
    //4,递归加载
    LoadXMLToTreeView(rootElement,rootElement.Nodes);

}
private static void LoadXMLToTreeView(XElement rootElement, TreeNodeCollection treeNodeCollection)
{
    //获取跟元素下的所有子元素
    //rootElement.Elements();
    //遍历所有子元素
    foreach (XElement item in rootElement.Elements())
    {
        if (item.Elements().Count() == 0)
        {
            //item.Value;
            //第一种写法
            //treeNodeCollection.Add(item.Name,ToString());
            //treeNodeCollection.Add(item.Value);

            //第二种写法
            //treeNodeCollection.Add(item.Name,ToString()).Nodes.Add(item.Value);

        }
        else
        {
            //将当前子元素加载到TreeView的节点集合中
            //TreeNode node=treeNodeCollection.Add(item.Name,ToString());
            //LoadXMLToTreeView(item,node.Nodes);
        }
    }
}
XDocument方式读取xml加载到TreeView节点上
public static void ReadXMLRecursionByXMLDocument()
{
    string xmlPath = AppDomain.CurrentDomain.BaseDirectory;
    xmlPath = xmlPath.Replace("bin\\Debug", "");
    string filePath = xmlPath + "xml\\Person.xml";
    //1,读取xml文件(XDocument)兼容2.0以上版本
    XmlDocument xDoc = new XmlDocument();

    //2,获取根节点
    xDoc.Load("../../xml/Person.xml");

    //3,将xml的根元素加载到TreeView的根节点上
    XmlElement rootElement = xDoc.DocumentElement;
    TreeNode rootNode = treeView.Nodes.Add(rootElement.Name);
    //4,递归加载
    LoadXMLToTreeViewByXMLDocument(rootElement, rootElement.Nodes);

}

private static void LoadXMLToTreeViewByXMLDocument(XmlElement rootElement,TreeNodeCollection treeNodeCollection)
{
   //循环rootElement下的所有子元素加载到treeNodeCollection集合中
   foreach (XmlNode item in rootElement.ChildNodes)
   {
       //在继续之前需要判断一下当前节点是什么类型的节点
       if (item.NodeType == XmlNodeType.Element)
       {
           //如果当前节点是一个 元素 节点,则把当前节点加载到TreeView上
           TreeNode node= treeNodeCollection.Add(item.Name);
           LoadXMLToTreeViewByXMLDocument((XmlElement)item, node.Nodes);
       } else if (item.NodeType==XmlNodeType.Text|item.NodeType==XmlNodeType.CDATA) {
           treeNodeCollection.Add(item.InnerText);
       }
   }
}

读取的xml文件Person.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<List>
  <Person id="1">
    <Name>张三</Name>
    <Age>17</Age>
    <Email>zhangsan123@qq.com</Email>
  </Person>
  <Person id="2">
    <Name>李四</Name>
    <Age>18</Age>
    <Email>lisi123@qq.com</Email>
  </Person>
  <Person id="3">
    <Name>王五</Name>
    <Age>19</Age>
    <Email>wangwu123@qq.com</Email>
  </Person>
  <Person id="4">
    <Name>赵六</Name>
    <Age>20</Age>
    <Email>zhaoliu123@qq.com</Email>
  </Person>
  <Person id="5">
    <Name>刘备</Name>
    <Age>21</Age>
    <Email>liubei123@qq.com</Email>
  </Person>
</List>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值