0

i have the following data

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

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.

Thank you in advance

Vince
  • 165
  • 1
  • 14
  • i have tried http://stackoverflow.com/questions/6945216/converting-flattened-hierarchical-data-from-sql-server-into-a-structured-json-ob – Vince Dec 08 '11 at 01:28
  • my previous post http://stackoverflow.com/questions/8421822/hierarchy-from-char-delimited-string – Vince Dec 08 '11 at 01:30

1 Answers1

2

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);
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • wonder if i could bother you one more time, and see if there is a way to also include a count with the dictionary? Lets suppose there is a count of files for each line, how easily could it be added to the dictionary?? – Vince Dec 08 '11 at 18:07
  • @Vince: Fairly easy; add FileCount to the NodeEntry class and pass the value to the AddEntry method. You will need to change the AddEntry method to a method of type boolean and only return true when a new entry is added to the collection. Then if oItem.Children.AddEntry returns false, set FileCount in oItem to your passed in FileCount since this will be the last item. – competent_tech Dec 08 '11 at 18:12
  • Thank you, as the name says the best answers i have had ever. – Vince Dec 08 '11 at 18:15