天天看點

linq to xml/動态類型 從樹型表建構樹(linq to entity處理資料庫)

有疑問的可以去下面位址聯系我 歡迎拍磚!

http://bbs.ctrlc.com.cn/forum.php?mod=viewthread&tid=594&extra=page%3D1

資料庫

linq to xml/動态類型 從樹型表建構樹(linq to entity處理資料庫)

linq to xml方法

public dynamic GetAllTree()
        {
            List<MenuClassify> roots = GetAllRoot();//取得所有根節點


            //建立xml
            XDocument tree = new XDocument();
            tree.Declaration = new XDeclaration("1.0", "utf-8", "no");
            tree.Add(new XElement("Tree"));
   
            //添加樹
            foreach (var menuClassify in roots)
            {
                XElement node=new XElement("Node");//添加節點
                node.SetAttributeValue("name", menuClassify.Name);//為節點添加屬性
                node.SetAttributeValue("id", menuClassify.MenuClassifyID);
                tree.Element("Tree").Add(node);//将節點加入樹
                NextNode(menuClassify.MenuClassifyID,node);//開始遞歸
            } 


            string xmlstr = tree.ToString();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlstr);
            string jsonStr = JsonConvert.SerializeXmlNode(doc);


            return tree;   
        }
           
 public void NextNode(int id,XElement parentNode)
        {
            var nodes = DBHelper.Entity.MenuClassifies.Where(m => m.ParentID == id).Select(m => m);//獲得次父節點的所有子節點


            foreach (var node in nodes)
            {                              
                XElement xNode=new XElement("Node");
                parentNode.Add(xNode);
                xNode.SetAttributeValue("name",node.Name);
                xNode.SetAttributeValue("id",node.MenuClassifyID);
                NextNode(node.MenuClassifyID,xNode);//遞歸周遊
            }
        }
           

dynamic方式建構

public dynamic GetAllTree()
        {
            List<MenuClassify> roots = GetAllRoot();//取得所有根節點

            List<dynamic> tree=new List<dynamic>();

            foreach (var root in roots)
            {
                dynamic node = new ExpandoObject();//建立節點
                node.name = root.Name;//為屬性指派
                node.id = root.MenuClassifyID;
                tree.Add(node);//将節點加入樹
                NextNode(root.MenuClassifyID,node);//開始遞歸
            }

            string jsonstr = JsonConvert.SerializeObject(tree);//test部分 序列化成json

            return tree;
        }

        public void NextNode(int id,dynamic parentNode)
        {
            var nodes = DBHelper.Entity.MenuClassifies.Where(m => m.ParentID == id).Select(m => m);

            if (nodes.ToList().Count != 0)//存在子節點
            {
                List<dynamic> list = new List<dynamic>();
                parentNode.nodes = list;
                foreach (var node in nodes)
                {
                    dynamic n = new ExpandoObject();
                    n.name = node.Name;
                    n.id = node.MenuClassifyID;
                    list.Add(n);

                    NextNode(node.MenuClassifyID, n);
                }
            }
        }