I have a tree [textual] like this:
+
| +
| | +
| | \
| +
| \
+
Tree2
+
| \
| | +
| | \
+
This is just a small example: a tree can be deeper and have more children, etc.
Now I am doing this:
for (int i = 0; i < cmdOutList.Count; i++)
{
string s = cmdOutList[i];
String value = Regex.Match(s, @"(?<=\---).*").Value;
value = value.Replace("\r", "");
if (s[1].ToString() == "-")
{
DirectoryNode p = new DirectoryNode { Name = value };
directoryList.Add(p);
}
else
{
DirectoryNode f = new DirectoryNode { Name = value };
directoryList[i - 1].AddChild(f);
directoryList.Add(f);
}
}
But this does not handle "step_2.1" and "step_2.2"
I think that I am doing it completely wrong, maybe someone can help me with this.
EDIT :
Here is a class DirectoryNodeto make this clearer.
public class DirectoryNode
{
public DirectoryNode()
{
this.Children = new List<DirectoryNode>();
}
public DirectoryNode ParentObject { get; set; }
public string Name;
public List<DirectoryNode> Children { get; set; }
public void AddChild(DirectoryNode child)
{
child.ParentObject = this;
this.Children.Add(child);
}
}
value source
share