What is the best .NET library (commercial or open source) that implements a non-binary tree and its associated operations? The requirements are to dynamically insert and delete nodes, copy/paste nodes, find information buried in nodes, copy/paste of folders and their children from one area of the tree to another. The tree is at the business logic layer. Presentation layer is WPF. Implementation language is C#.
+2
A:
Trees are so easy to write, and specific requirements relatively diverse, that I'm not sure that a "tree library" would be very useful. Why don't you write your own?
Barry Kelly
2009-09-02 10:36:36
+4
A:
I would say LINQ to XML without a doubt.
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "true"),
new XComment("Comment"),
new XElement("Employees",
new XElement("RootElement",
new XElement("Employee",
new XAttribute("id", "123"),
new XElement("name", "John"),
new XCData("CData")))));
// Selection multiple nodes
var allEmployees = xdoc.Root.Elements("Employees");
// Select single node
var employeeJohn = from node in xdoc.Root.Descendants().Elements("Employees").Elements("Employee")
where node.Attribute("id").Value == "123"
select node;
// Insert node
XElement newNode = new XElement("NewNode", "Node content");
allEmployees.Add(newNode);
// Delete node
employeeJohn.Remove();
Seb Nilsson
2009-09-02 10:53:12
You should probably add an example of how you would dynamically insert and remove nodes on some existing tree.
Daniel Earwicker
2009-09-03 10:09:06
+4
A:
I would use:
class MyTreeNode : List<MyTreeNode>
{
// declare per-node properties here, e.g.
public string Name { get; set; }
}
Building and rearranging the tree is pretty straightforward:
MyTreeNode root = new MyTreeNode {Name = "root"};
MyTreeNode firstChild = new MyTreeNode {Name = "1"};
root.Add(firstChild);
MyTreeNode secondChild = new MyTreeNode { Name = "2" };
root.Add(secondChild);
root.Remove(firstChild);
secondChild.Add(firstChild);
Daniel Earwicker
2009-09-02 10:58:12