路径数据,如数据结构树 [英] path data to tree like data structure

查看:147
本文介绍了路径数据,如数据结构树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据

root
root/blue
root/blue/temp
root/main
root/main/dev
root/main/back
root/etc/init
root/etc/init/dev
root/etc/init/test
root/etc/init/back
root/etc/init/server
root/etc/init/system
root/etc/init/setup
root/system
root/system/temp1
root/system/temp2
root/system/temp3
root/system/temp4
root/system/temp5
root/system/temp5/dev1
root/rel
root/intel/archival
root/intel/archival/newsreel
root/intel/archival/recording

我想能够使用类要么数据绑定到一个树形控件(ASP.Net)或生成UL /李jQuery的消耗。

i would like to be able to use the class to either databind to a tree control (ASP.Net) or generate a UL/Li for jquery consumption.

我需要将其转换为一个列表类,将返回正确的层次结构。我已经尝试了许多不同的方法,到目前为止,我没有能够找到一个解决方案。我卡住了。我试着问在前面的文章,但该解决方案没有奏效,许多尝试修改一些它只是普通的不能正常工作了。我希望你们可以帮助我。

I need to convert it to a List class that will return the proper hierarchy. I have tried many different approach so far, and I'm not able to find a solution. I'm stuck. I tried asking in an earlier post but the solution did not work, after many attempts to modify some it just plain does not work. I hope one of you can help me out.

另外这不是一个简单的拆分功能,我知道如何分割字符串。

Also this is not a simple split functions, I know how to split a string.

感谢您提前

推荐答案

下面是产生的NodeEntry项目嵌套字典的解决方案:

Here is a solution that generates a nested dictionary of NodeEntry items:

public class NodeEntry
{
    public NodeEntry()
    {
        this.Children = new NodeEntryCollection();
    }

    public string Key { get; set; }
    public NodeEntryCollection Children { get; set; }

}

public class NodeEntryCollection : Dictionary<string, NodeEntry>
{
    public void AddEntry(string sEntry, int wBegIndex)
    {
        if (wBegIndex < sEntry.Length)
        {
            string sKey;
            int wEndIndex;

            wEndIndex = sEntry.IndexOf("/", wBegIndex);
            if (wEndIndex == -1)
            {
                wEndIndex = sEntry.Length;
            }
            sKey = sEntry.Substring(wBegIndex, wEndIndex - wBegIndex);
            if (!string.IsNullOrEmpty(sKey)) {
                NodeEntry oItem;

                if (this.ContainsKey(sKey)) {
                    oItem = this[sKey];
                } else {
                    oItem = new NodeEntry();
                    oItem.Key = sKey;
                    this.Add(sKey, oItem);
                }
                // Now add the rest to the new item's children
                oItem.Children.AddEntry(sEntry, wEndIndex + 1);
            }
        }
    }
}

要使用上面,创建一个新的集合:

To use the above, create a new collection:

        NodeEntryCollection cItems = new NodeEntryCollection();

然后,在你的列表中的每个行:

then, for each line in your list:

        cItems.AddEntry(sLine, 0);

这篇关于路径数据,如数据结构树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆