Best .NET library for trees
What's the best .NET library (commercial or open source) that implements a non-bin tree and associated operations? Requirements - dynamically insert and delete nodes, copy / paste nodes, find information similar to nodes, copy / paste folders and their children from one area of the tree to another. The tree is at the level of business logic. The presentation layer is WPF. The implementation language is C #.
+2
source to share
4 answers
I would say LINQ to XML , no doubt about it.
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();
+4
source to share
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);
+4
source to share