Csharp/C#教程:路径数据到树状数据结构分享


路径数据到树状数据结构

我有以下数据

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类,它将返回适当的层次结构。 到目前为止,我尝试了许多不同的方法,但我无法找到解决方案。 我被卡住了。 我尝试在较早的post中询问但是解决方案没有用,经过多次尝试修改一些它只是简单无效。 我希望你们中的一个可以帮助我。

这也不是一个简单的拆分函数,我知道如何拆分字符串。

先感谢您

这是一个生成NodeEntry项的嵌套字典的解决方案:

 public class NodeEntry { public NodeEntry() { this.Children = new NodeEntryCollection(); } public string Key { get; set; } public NodeEntryCollection Children { get; set; } } public class NodeEntryCollection : Dictionary { 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(); 

然后,对于列表中的每一行:

上述就是C#学习教程:路径数据到树状数据结构分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!

  cItems.AddEntry(sLine, 0); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/999748.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐