簡體   English   中英

路徑數據到樹狀數據結構

[英]path data to tree like data structure

我有以下數據

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)或生成用於jquery消耗的UL / Li。

我需要將它轉換為List類,它將返回適當的層次結構。 到目前為止,我嘗試了很多不同的方法,但我無法找到解決方案。 我被卡住了。 我嘗試在較早的帖子中詢問,但解決方案沒有用,經過多次嘗試修改一些它只是簡單無效。 我希望你們中的一個可以幫助我。

這也不是一個簡單的拆分函數,我知道如何拆分字符串。

先感謝您

這是一個生成NodeEntry項的嵌套字典的解決方案:

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);
            }
        }
    }
}

要使用上述內容,請創建一個新集合:

        NodeEntryCollection cItems = new NodeEntryCollection();

然后,對於列表中的每一行:

        cItems.AddEntry(sLine, 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM