easyui及读取xml
本地测试地址例如http://localhost:6541/TreeExam/AuthorityTree
TreeExam 是TreeExamController
AuthorityTree是TreeExamController内的AuthorityTree()方法
InitTree()方法未读取xml
InitTree2()和InitTree3()两种方法读取xml
----------------------Views---------AuthorityTree.cshtml------------------------------
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AuthorityTree</title>
    @*下载easyui 包 引入*@
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/Jquery_EasyUI/jQuery.easyui.1.2.2 Demo/js/themes/default/easyui.css")" />
    <link rel="stylesheet" type="text/css" href="@Url.Content("~/Content/Jquery_EasyUI/jQuery.easyui.1.2.2 Demo/js/themes/icon.css")" />
     <script src="@Url.Content("~/Content/Jquery.js")" type="text/javascript"></script>
     <script type="text/javascript" src="@Url.Content("~/Content/Jquery_EasyUI/jQuery.easyui.1.2.2 Demo/js/jquery.easyui.min.1.2.2.js")"></script>
     <script type="text/javascript">
         $(function () {
             $('#authorityTbl').treegrid({
                 title: '权限列表',
                 iconCls: 'icon-save',
                 width: $(document).width() * 0.80,
                 height: 500,
                 nowrap: false,
                 animate: true,
                 collapsible: true,
                 loadMsg: "数据加载中,请稍后...",
                 fitColumns: true,
                 url: '@Url.Content("~/TreeExam/InitTree3")',@*InitTree,InitTree2,InitTree3分别是三种方式实现easyui*@
                 idField: 'AuthorityID',
                 treeField: 'AuthorityName',
                 columns: [[
                    { field: 'AuthorityID', title: '权限id', width: 200 },
                    { field: 'AuthorityName', title: '权限名称', width: 300 },
                     { field: 'Remark', title: '备注', width: 200 },
                     { field: 'ParentID', title: '父级', width: 200 }
                ]],
                 toolbar: [{
                     text: '',
                     iconCls: 'icon-add',
                     handler: function () {
}
                 }, '-', 
                 {
                 text:'',
                 iconCls:'icon-edit',
                 handler:function(){
}
             }, '-',
                 {
                     text: '',
                     iconCls: 'icon-remove',
                     handler: function () {
}
                 }, '-'],
                 onLoadSuccess: function () {
                     $('#authorityTbl').treegrid('collapseAll');
                 }
             })
         })  
    </script>
</head>
<body>
   <div id="listDiv">  
    <table id="authorityTbl"  toolbar="#searchbar" class="easyui-treegrid" tbl="list">  
    </table>  
</div> 
</body>
</html>
----------------------Controller---------TreeExamController.cs--------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Xml;
using System.Data;
namespace MvcStudy3.Controllers
{
    public class TreeExamController : Controller
    {
//[HttpGet]
        public ActionResult AuthorityTree() 
        {
            return View();
        }
#region InitTree()
        public string InitTree()
        {
            string result = "";
            IList<Authority> list = GetAuthorityList("",4);
foreach (Authority node in list)
            {
                result += Recursion(node) + ",";
            }
            return "[" + result.TrimEnd(',') + "]";
        }
// 递归树形  
        public string Recursion(Authority model)
        {
            string res_s = "";
            //你想要在视图中得到的值  
            res_s += "{\"AuthorityID\":\"" + model.AuthorityID + "\",\"AuthorityName\":\"" +
                model.AuthorityName + "\",\"ParentID\":\"" + model.ParentID +
                "\",\"iconPath\":\"" + model.IconPath + "\",\"AuthorityPath\":\"" + model.AuthorityPath + 
                "\",\"Remark\":\"" + model.Remark + "\"";
IList<Authority> list = GetAuthorityList(model.AuthorityID, 3);
            if (list != null)
            {
                res_s += "," + "\"children\":[";
                for (int i = 0; i < list.Count; i++)
                {
                    if (i > 0)
                        res_s += ",";
                    res_s += Recursion2(list[i]);
                }
                res_s += "]";
            };
            res_s += "}";
            return res_s;
        }
public string Recursion2(Authority model)
        {
            string res_s = "";
            //你想要在视图中得到的值  
            res_s += "{\"AuthorityID\":\"" + model.AuthorityID + "\",\"AuthorityName\":\"" +
                model.AuthorityName + "\",\"ParentID\":\"" + model.ParentID +
                "\",\"iconPath\":\"" + model.IconPath + "\",\"AuthorityPath\":\"" + model.AuthorityPath +
                "\",\"Remark\":\"" + model.Remark + "\"";
            res_s += "}";
            return res_s;
        }
public IList<Authority> GetAuthorityList(string pid, int c)
        {
            IList<Authority> list = new List<Authority>();
            for (int i = 0; i < c; i++)
            {
                list.Add(getAuthority(pid, i));
            }
            return list;
        }
/// <summary>
        /// 
        /// </summary>
        /// <param name="pid">父目录id</param>
        /// <param name="c">需要的个数</param>
        /// <returns></returns>
        public Authority getAuthority(string pid,int i) 
        {
            Authority a = new Authority();
            if (string.IsNullOrEmpty(pid))
            {
                a.AuthorityID = i.ToString();
                a.AuthorityName = i+"name";
                a.AuthorityPath = i+"authoritypath";
                a.IconPath = i+"iconpath";
                a.ParentID = "0";
                a.Remark = i+"remark";
            }
            else 
            {
                a.AuthorityID = pid+""+i;
                a.AuthorityName =  pid+""+i+"name";
                a.AuthorityPath = pid + "" + i + "authoritypath";
                a.IconPath = pid + "" + i + "iconpath";
                a.ParentID = pid;
                a.Remark = pid + "" + i + "remark";
            }
            return a;
        }
        #endregion
        #region InitTree2()
        public string InitTree2()
        {
            string result = "";
            IList<Authority> list = getXml();
            IList<Authority> ll = getSelectList(list,"0");
            foreach (Authority node in ll)
            {
                result += Recursion1(node,list) + ",";
            }
            return "[" + result.TrimEnd(',') + "]";
        }
// 递归树形  
        public string Recursion1(Authority model,IList<Authority> list)
        {
            string res_s = "";
            //你想要在视图中得到的值  
            res_s += "{\"AuthorityID\":\"" + model.AuthorityID + "\",\"AuthorityName\":\"" +
                model.AuthorityName + "\",\"ParentID\":\"" + model.ParentID +
                "\",\"iconPath\":\"" + model.IconPath + "\",\"AuthorityPath\":\"" + model.AuthorityPath +
                "\",\"Remark\":\"" + model.Remark + "\"";
IList<Authority> list2 = getSelectList(list,model.AuthorityID);
            if (list2 != null)
            {
                res_s += "," + "\"children\":[";
                for (int i = 0; i < list2.Count; i++)
                {
                    if (i > 0)
                        res_s += ",";
                    res_s += Recursion1(list2[i],list);
                }
                res_s += "]";
            };
            res_s += "}";
            return res_s;
        }
public IList<Authority> getSelectList(IList<Authority> list,string pid) 
        {
            IList<Authority> ll =list.Where(p => p.ParentID == pid).ToList<Authority>();//用Where可以筛选list内容,Select不能筛选
            return ll;
        }
public IList<Authority> getXml()
        {
             IList<Authority> list = new List<Authority>();
            XmlNode node;
            XmlDocument xd = new XmlDocument();//定义一个xmldocument对象
            string xmlpath = Server.MapPath("~/Files/XMLFile1.xml");//加载xml文件
            xd.Load(@xmlpath);
            XmlElement tree = xd.DocumentElement;
            for (int i = 0; i < tree.ChildNodes.Count; i++)//对根子节点的所有子节点进行循环
            {
                node = tree.ChildNodes[i];
if (node.HasChildNodes)
                {
                    XmlNode childnode = node.ChildNodes[0];
                    getAuthority(childnode,node,list);                    
                }
            }
            return list;
        }
public IList<Authority> getAuthority(XmlNode childnode, XmlNode node, IList<Authority> list)
        {
            Authority a = new Authority();
            a.AuthorityID = childnode.InnerText;
            childnode = node.ChildNodes[1];
            a.AuthorityName = childnode.InnerText;
            childnode = node.ChildNodes[3];
            a.ParentID = childnode.InnerText;
            childnode = node.ChildNodes[5];
            a.Remark = childnode.InnerText;
            if (node.ChildNodes.Count > 6)
            {
                childnode = node.ChildNodes[6];
                if (childnode.HasChildNodes)
                {
                    XmlNode childn = childnode.ChildNodes[0];
                    getAuthority(childn, childnode, list);
                }
            }
            list.Add(a);
            return list;
        }
        #endregion
#region 读取xml ds.ReadXml(xmlpath);
        public string InitTree3()
        {
            string result = "";
            DataSet ds = new DataSet();
            string xmlpath = Server.MapPath("~/Files/XMLFile1.xml");//xml文件的相对位置
            ds.ReadXml(xmlpath);
            IList<Authority> list = getXml3(ds);
foreach (Authority node in list)
            {
                result += Recursion3(node,ds,1) + ",";
            }
            return "[" + result.TrimEnd(',') + "]";
        }
public IList<Authority> getXml3(DataSet ds)
        {
            IList<Authority> list = new List<Authority>();
            DataTable dt = ds.Tables[0];//获取第一级的节点内容
            foreach (DataRow dr in dt.Rows)
            {
                Authority a = new Authority();
                a.AuthorityID = dr["AuthorityID"].ToString();
                a.AuthorityName = dr["AuthorityName"].ToString();
                a.ParentID=dr["ParentID"].ToString();
                a.Remark = dr["Remark"].ToString();
                list.Add(a);
            }
            return list;
        }
        // 递归树形  
        public string Recursion3(Authority model,DataSet ds,int count)
        {
            string res_s = "";
            //你想要在视图中得到的值  
            res_s += "{\"AuthorityID\":\"" + model.AuthorityID + "\",\"AuthorityName\":\"" +
                model.AuthorityName + "\",\"ParentID\":\"" + model.ParentID +
                "\",\"iconPath\":\"" + model.IconPath + "\",\"AuthorityPath\":\"" + model.AuthorityPath +
                "\",\"Remark\":\"" + model.Remark + "\"";
IList<Authority> list = GetAuthorityList(model.AuthorityID, ds,count);
            if (list != null)
            {
                res_s += "," + "\"children\":[";
                for (int i = 0; i < list.Count; i++)
                {
                    if (i > 0)
                        res_s += ",";
                    res_s += Recursion3(list[i],ds,count+1);
                }
                res_s += "]";
            };
            res_s += "}";
            return res_s;
        }
public IList<Authority> GetAuthorityList(string aid,DataSet ds,int count) 
        {
             IList<Authority> list = new List<Authority>();
             if (ds.Tables.Count > count)
             {
                 DataTable dt = ds.Tables[count];
                 DataRow[] drlist = dt.Select(string.Format("ParentID='{0}'", aid));
                 foreach (DataRow dr in drlist)
                 {
                     Authority a = new Authority();
                     a.AuthorityID = dr["AuthorityID"].ToString();
                     a.AuthorityName = dr["AuthorityName"].ToString();
                     a.ParentID = dr["ParentID"].ToString();
                     a.Remark = dr["Remark"].ToString();
                     list.Add(a);
                 }
                 return list;
             }
             else 
             {
                 return null;
             }
        }
        #endregion
    }
public class Authority
    {
        public string AuthorityID { get; set; }
        public string AuthorityName { get; set; }
        public string IconPath { get; set; }
        public string ParentID { get; set; }
        public string AuthorityPath { get; set; }
        public string Remark { get; set; }
    }
}
----------------XMLFile1.xml 文件----------------------------------------------
红色部分是节点名称,可以随便命名
<?xml version="1.0" encoding="utf-8" ?>
<authority>
  <a>
    <AuthorityID>1</AuthorityID>
    <AuthorityName>1name</AuthorityName>
    <IconPath>1iconpath</IconPath>
    <ParentID>0</ParentID>
    <AuthorityPath>1authoritypath</AuthorityPath>
    <Remark>1remak</Remark>
    <b>
      <AuthorityID>11</AuthorityID>
      <AuthorityName>11name</AuthorityName>
      <IconPath>11iconpath</IconPath>
      <ParentID>1</ParentID>
      <AuthorityPath>11authoritypath</AuthorityPath>
      <Remark>11remak</Remark>
      <c>
        <AuthorityID>111</AuthorityID>
        <AuthorityName>111name</AuthorityName>
        <IconPath>111iconpath</IconPath>
        <ParentID>11</ParentID>
        <AuthorityPath>111authoritypath</AuthorityPath>
        <Remark>111remak</Remark>
      </c>
    </b>
  </a>
  <a>
    <AuthorityID>2</AuthorityID>
    <AuthorityName>2name</AuthorityName>
    <IconPath>2iconpath</IconPath>
    <ParentID>0</ParentID>
    <AuthorityPath>2authoritypath</AuthorityPath>
    <Remark>2remak</Remark>
    <b>
      <AuthorityID>21</AuthorityID>
      <AuthorityName>21name</AuthorityName>
      <IconPath>21iconpath</IconPath>
      <ParentID>2</ParentID>
      <AuthorityPath>21authoritypath</AuthorityPath>
      <Remark>21remak</Remark>
    </b>
  </a>
  <a>
    <AuthorityID>3</AuthorityID>
    <AuthorityName>3name</AuthorityName>
    <IconPath>3iconpath</IconPath>
    <ParentID>0</ParentID>
    <AuthorityPath>3authoritypath</AuthorityPath>
    <Remark>3remak</Remark>
    <b>
      <AuthorityID>31</AuthorityID>
      <AuthorityName>31name</AuthorityName>
      <IconPath>31iconpath</IconPath>
      <ParentID>3</ParentID>
      <AuthorityPath>31authoritypath</AuthorityPath>
      <Remark>31remak</Remark>
    </b>
  </a>
</authority>
easyui及读取xml的更多相关文章
- 读取xml数据装配到字典中之应用场景
		前段时间看到支付宝设置里面有个多语言这个功能,蛮有意思的,就想双休没事的话做个相关的demo玩玩,可是礼拜六被妹子拽出去玩了一天,来大上海有大半年了,基本没有出去玩过,妹子说我是超级宅男,也不带她出去 ... 
- 自己动手之使用反射和泛型,动态读取XML创建类实例并赋值
		前言: 最近小匹夫参与的游戏项目到了需要读取数据的阶段了,那么觉得自己业余时间也该实践下数据相关的内容.那么从哪入手呢?因为用的是Unity3d的游戏引擎,思来想去就选择了C#读取XML文件这个小功能 ... 
- MFC如何读取XML
		<?xml version="1.0" encoding="utf-8"?> <Cases> <case> <No&g ... 
- 使用dom4j读取xml连接数据库与之单例模式
		使用dom4j读取xml ,加入jar包 dom4j-1.6.1.jar jaxen-1.1-beta-6.jar public class XmlConfigReader { //懒汉式,延迟加载 ... 
- java DOM4J 读取XML
		最近学习Java,在处理XML文档的时候,查阅相关资料,发现了DOM4J这个jre库,相对C#的XML处理来说,功能还算是跟得上 下面展示一篇我自己写的一个XML读取测试 import java.ut ... 
- C#中常用的读取xml的几种方法(转)
		本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ... 
- wcf序列化大对象时报错:读取 XML 数据时,超出最大
		错误为: 访问服务异常:格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出 错: request.InnerException 消息是“反序 ... 
- C#中常用的几种读取XML文件的方法
		1.C#中常用的几种读取XML文件的方法:http://blog.csdn.net/tiemufeng1122/article/details/6723764/ 
- 利用反射与dom4j读取javabean生成对应XML和读取XML得到对应的javabean对象集合
		转自:http://blog.csdn.net/zhao19861029/article/details/8473245 首先实现生成对应的JAVAbean的XML文件方法 /** * DMO4J写入 ... 
随机推荐
- Linux网络服务第一章Linux网络基础设置
			1.笔记 systenctl restart network:重启网卡网络服务 bash:刷新主机名称 netstat:查看网络状态 route -n:不做地址解析 mii-tool eno16777 ... 
- PL/SQL登录Oracle18数据库:ORA-12154:TNS无法解析指定的连接标识符
			PL/SQL登录Oracle18数据库出现ORA-12154:TNS无法解析指定的连接标识符解决以下问题:首先更改Oracle客户端的tnsnames.ora,我的路径是:D:\OracleSQL\n ... 
- spark下dataframe转为rdd格式
			dataframe可以实现很多操作,但是存储到本地的时候,只能存 parquest格式 需要存储源格式,需要转换为rdd类型 将dataframe中的每一行都map成有逗号相连的string,就变为了 ... 
- Ribbon 框架简介及搭建
			2019独角兽企业重金招聘Python工程师标准>>> Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.UDP等 3. 提供负 ... 
- 几个加速Swift开发的小tip
			又是周五了,周末不要浪,一起学点Swift!本周再次为大家带来了一些Swift的小技巧,都是些奇淫巧计,不知道也无妨,但Swift最吸引我的一点就是它的简洁易用.主要内容有: private(set) ... 
- #Week1 Introduction
			一.What is Machine Learning 课程里主要给了两个供参考的定义: By Arthur Samuel: Field of study that gives computers th ... 
- INTERVIEW #0
			一.造成网络延迟的可能原因 1,WiFi所有用户上下行流量共用一个信道,当用户太多或者有人在下载大的资源时带宽不够,丢包: 2,线路质量不佳导致信噪比太低,比如光纤损耗太大等. 二.IPv6优势 1, ... 
- 从0开始学自定义View -1
			PS:好久没有写博客了,之前的东西有所忘记,百度一下竟然查到了自己的写过的博客,访问量还可以,一开始的写博客的初衷是把自己不会的记录下来,现在没想到也有博友会关注我,这就给了我动力,工作之余把零零碎碎 ... 
- python:入门
			基础学习路线 解释器 什么是解释器? 下载解释器 下载链接 安装解释器 PyCharm 什么是PyCharm? 下载安装 下载链接 参考:1 注意:选择社区版 PyCharm的基本操作 1.创建项目 ... 
- Awareness Kit让你的音乐APP脱颖而出,更懂用户,也更动人心
			让你的音乐APP脱颖而出,更懂用户,也更动人心. 场景 情景感知服务能带来什么? 作为音乐发烧友,闲下来的时候总想打开App,享受沉浸在音乐中的放松.然而,App推荐的歌单经常没法满足我的需要,如 ... 
