日期:2014-05-17  浏览次数:20735 次

树遍历
要做一个xml格式的功能列表
 数据库字段差不多是这样的
DataTable,其中有Id列、父Id列和内容Item列,具体数据如下:

Id Pid Item
1 0 A
2 1 B
3 1 C
4 2 D
5 2 E
6 3 F
7 3 G
8 3 H
取出数据以后怎么遍历 以及转成xml格式。

------解决方案--------------------
用SQL Connector读出来以后,应该是保存在DataTable里面的,
1:遍历
foreach(DataRow row in dt.Rows)
{
do something...
}
2.使用XMLWriter可以把DataTable直接转成XML格式,保存到硬盘上
------解决方案--------------------
还是如用代码直接加吧..
代码比较简单
------解决方案--------------------
先根据PID为0的节点创建根节点。再根据PID获取子节点向根节点添加子节点。如此循环。如果怕效率不够的话,可以考虑用算法优化一下。
------解决方案--------------------
C# code

TreeView1.Nodes.Clear();
        List<CYXTMS.Model.View_YM_GN> nodeList = viewBLL.GetList((int)vupModel.GN_ID);
        TreeNode temp;
        foreach (var node in nodeList)
        {
            temp = new TreeNode(node.GN_MC, node.GN_ID.ToString());
            if (!string.IsNullOrEmpty(node.YM_URL))
            {
                temp.NavigateUrl = node.YM_URL;
                temp.Target = "tabFrame";
            }
            else
            {
                temp.SelectAction = TreeNodeSelectAction.Expand;
            }
            TreeView1.Nodes.Add(temp);
        }
        IntiChildNodes(TreeView1.Nodes);
        foreach (TreeNode node in TreeView1.Nodes)
        {
            node.ImageUrl = node.ChildNodes.Count > 0 ? "~/images/themes/default/images/tree/folder.gif" 
                : "images/themes/default/images/tree/leaf.gif";
        }





 /// <summary>
    /// 递归绑定功能
    /// </summary>
    /// <param name="nodes"></param>
    private void IntiChildNodes(TreeNodeCollection nodes)
    {
        List<CYXTMS.Model.View_YM_GN> viewList;
        TreeNode tempChild;
        int tempID = -1;
        foreach (TreeNode item in nodes)
        {
            if (int.TryParse(item.Value ,out tempID))
            {
                viewList = viewBLL.GetList(tempID);
                foreach (var treeNode in viewList)
                {
                    tempChild = new TreeNode(treeNode.GN_MC, treeNode.GN_ID.ToString());
                    if (!string.IsNullOrEmpty(treeNode.YM_URL))
                    {
                        tempChild.NavigateUrl = treeNode.YM_URL;
                        tempChild.Target = "tabFrame";
                    }
                    else
                    {
                        tempChild.SelectAction = TreeNodeSelectAction.Expand;
                    }
                    item.ChildNodes.Add(tempChild);
                    if (int.TryParse(tempChild.Value, out tempID))
                    {
                        if (gnBLL.GetCount(tempID) > 0)
                        {
                            tempChild.ImageUrl = "images/themes/default/images/tree/folder.gif";
                            IntiChildNodes(item.ChildNodes);
                        }
                        else
                        {
                            tempChild.ImageUrl = "images/themes/default/images/tree/leaf.gif";
                        }
                    }
                }
            }
        }
    }