大家帮我看看这个方法有什么错误 谢谢
service.Path = { "123/aaa/dd ", "123/aa ", "333/555 "};存的类似这样的数据
ServiceInfo 和TreeModel 是自定义对象
最后要的结果是
123
aaa
dd
aa
33
55
我这个方法只能生成
123
aaa
aa
33
55
就是 "123/111/1222/232 "
"123/111/22 "
"22/232 "
这样的三个字符串 变成
123
111
1222
232
22
22
232
的结构
foreach (ServiceInfo service in ServiceItems)
{
string[] strData = service.Path.Split(new char[1] { '/ ' });
for (int i = 0; i < strData.Length; i++)
{
//当前节点
TreeModel treeModel = new TreeModel(strData[i].Trim());
//根节点,找出第一层
if (i == 0)
{
//到集合中找是否有这个名字
if (!FindRootInCollection(treeModel))
{
TreeModelItems.Add(treeModel);
}
else
{
//父节点
TreeModel parentModel = new TreeModel(strData[i - 1].Trim());
//当前节点是否已经在父节点的子集合中
if (!IsExitInChildList(parentModel, treeModel))
{
//加入到父节点的子集合中
AddTreeModel(parentModel, treeModel);
}
}
}
}
//就是这个方法有问题
/// <summary>
/// 加节点到父节点的子集合中
/// </summary>
/// <param name= "parentModel "> </param>
/// <param name= "currentModel "> </param>
private void AddTreeModel(TreeModel parentModel, TreeModel currentModel)
{
foreach (TreeModel model in TreeModelItems)
{
if (parentModel.Name == model.Name)
{
model.ChildList.Add(currentModel);
}
}
}
/// <summary>
/// 节点是否已经在父节点的子集合中
/// </summary>
/// <returns> </returns>
private bool IsExitInChildList(TreeModel parentModel, TreeModel currentModel)
{
bool bnlResult = false;
foreach (TreeModel model in parentModel.ChildList)
{
if (currentModel.Name == model.Name)
{
bnlResult = true;
}
}
return bnlResult;
}
private bool FindRootInCollection(TreeModel treeModel)
{
bool bnlResult = false;
foreach (TreeModel model in TreeModelItems)
{
if (treeModel.Name == model.Name)
{
bnlResult = true;
}
}
return bnlResult;
}
[解决办法]
model.ChildList.Add(currentModel);
改为
model.Nodes.Add(currentModel);
[解决办法]
不知道。帮顶
[解决办法]
meishijan