原文发布时间为:2008-08-10 —— 来源于本人的百度文章 [由搬家工具导入]

using System.Xml;

protected void Button2_Click(object sender, EventArgs e)
    {
        string xmlpath = Server.MapPath("~/App_Data/dirList.xml");
        if (File.Exists(xmlpath))
        {
            XmlDocument dom = new XmlDocument();
            dom.Load(xmlpath);
            TreeView1.Nodes.Clear();

            BindXmlToTreeView(dom.DocumentElement.ChildNodes, TreeView1.Nodes);
        }
        else
        {
            Response.Write("<script>alert('XML文档不存在,请先创建')</script>");
        }
    }

    protected void BindXmlToTreeView(XmlNodeList xmlnodes, TreeNodeCollection treeNodes)
    {
        foreach (XmlNode child in xmlnodes)
        {
            if (child.Attributes != null && child.Attributes.Count > 0)//这个判断很重要!
            {
                string treeText = child.Attributes["name"].Value;
                TreeNode tn = new TreeNode(treeText);
                treeNodes.Add(tn);
                BindXmlToTreeView(child.ChildNodes, tn.ChildNodes);
            }
        }
    }

-----------------------------------------

原算法:

private void populateTreeControl(      System.Xml.XmlNode document,    System.Windows.Forms.TreeNodeCollection nodes)

{    

foreach (System.Xml.XmlNode node in          document.ChildNodes)    

{         // If the element has a value, display it;        

   // otherwise display the first attribute      

    // (if there is one) or the element name     

     // (if there isn't)        

string text = (node.Value != null ? node.Value :            (node.Attributes != null &&             node.Attributes.Count > 0) ?             node.Attributes[0].Value : node.Name);        

TreeNode new_child = new TreeNode(text);       

nodes.Add(new_child);        

populateTreeControl(node, new_child.Nodes);     

}  

}

xml文档绑定某个属性值到treeview算法的更多相关文章

  1. 使用JAXP对XML文档进行DOM解析

    import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers. ...

  2. XMLHelper类 源码(XML文档帮助类,静态方法,实现对XML文档的创建,及节点和属性的增、删、改、查)

    以下是代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  3. XML文档操作集锦(C#篇)

    在JSON没流行起来的时候xml一直作为程序存储配置信息的主流介质:特别是小型数据表方面还是不错的选择,所以经常涉及到的操作无非也就是增删改查,这篇博客主要是对这些对比较常用的操作做了个简单的总结 文 ...

  4. 根据Attribute值条件对XML文档进行修改

    现手上有一个XML文档, 需要把"直接工序"改为"间接工序0". 你可以使用<对XML文档进行修改> http://www.cnblogs.com/ ...

  5. 用Castor 处理XML文档

    ——Castor可以完成Java和XML的相互转换 前面有介绍过json-lib这个框架,在线博文:http://www.cnblogs.com/hoojo/archive/2011/04/21/20 ...

  6. XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation

    文章转载自:https://yq.aliyun.com/articles/40353 相信很多人和我一样,在编写Spring或者Maven或者其他需要用到XML文档的程序时,通常都是将这些XML文档头 ...

  7. 关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

    https://yq.aliyun.com/articles/40353 ************************************* 摘要: 相信很多人和我一样,在编写Spring或者 ...

  8. Spring学习----- Spring配置文件xml文档的schema约束

    1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  9. 关于Spring配置文件xml文档的schema约束

    最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version=&q ...

随机推荐

  1. Bootstrap历练实例:嵌套的媒体对象

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. Nginx正向代理代理http和https服务

    Nginx正向代理代理http和https服务 1. 背景需求 通过Nginx正向代理,去访问外网.可实现局域网不能访问外网的能力,以及防止在上网行为上,留下访问痕迹. 2. 安装配置 2.1安装 w ...

  3. IOS中将颜色转换为image

    - (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f ...

  4. JS数据结构与算法--单向链表

    链表结构:链表中每个元素由一个存储元素本身的节点和一个指向下一元素的引用组成.如下所示(手画的,比较丑,懒得用工具画了,嘻嘻) 1.append方法,向链表末尾插入一个节点 2.insert(posi ...

  5. 2019.5.18-5.19 ACM-ICPC 全国邀请赛(西安)赛后总结

    第一次出去比赛经验太少了!!!果然最大目的是长见识和受刺激Orz 以下流水账: 背了本两千两百页的牛津高阶英汉双解词典,背了吃的,背了衣服……以后这些东西统统不带,买本口袋词典即可.上述物品这次比赛全 ...

  6. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

  7. 初识Mysql之基本简单语法总结

    一.  DDL(data definition language)语句:数据定义语言. 这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象.常用语句关键字:create.drop.alter ...

  8. vim小操作

    初时,先有ed,ed为ex之父,ex为vi之父,而vi为vim之父 c 修改 d 删除 y 复制到寄存器 g~ 反转大小写 gu 反转为小写 gU 反转为大写 > 增加缩进 < 减小缩进 ...

  9. 【laravel】Disabling CSRF for Specific Routes - Laravel 5

    原文 http://www.camroncade.com/disable-csrf-for-specific-routes-laravel-5/ Disabling CSRF for Specific ...

  10. matplotlib学习记录 三

    # 绘制自己和朋友在各个年龄的女友数量的折线图 from matplotlib import pyplot as plt # 让matplotlib能够显示中文 plt.rcParams['font. ...