2011-12-08 20 views
0

I有しiはいずれかのツリーコントロール(ASP.Net)にデータバインドまたはUL /リチウムを生成するためにクラスを使用できるようにしたい経路データ

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 

次データjquery消費のために。

適切な階層を返すListクラスに変換する必要があります。私はこれまでにいろいろなアプローチを試みましたが、私は解決策を見つけることができません。私は立ち往生している。私は以前の記事で尋ねてみましたが、解決策がうまくいきませんでした。私の一人が私を助けてくれることを願っています。

また、これは単純な分割関数ではなく、文字列を分割する方法を知っています。

はここで事前

+0

私は試してみましたhttp://stackoverflow.com/questions/6945216/converting-flattened-hierarchical-data-from-sql-server-into-a-structured-json-ob – Vince

+0

私の前の投稿http:// stackoverflow。 com/questions/8421822/hierarchy-from-char-delimited-string – Vince

答えて

2

にありがとう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); 
+0

私はすぐにそれを試していただきありがとうございます。 – Vince

+0

完璧な答え、ありがとうございます – Vince

+0

私はあなたにもう一度お困りかと思っており、辞書にカウントを含める方法があるかどうかを見ていますか?各行にファイルの数があるとします。どのくらい簡単に辞書に追加できますか? – Vince