-2

I can't find an answer to this question no matter how I ask it. There appears to be solutions if the treeview is in WinForms but not if it's in WPF.

I'm loading an XML file at runtime that is bound to a treeview called OOB. The user can perform all kinds of edits to the treeview (adding and deleting nodes, changing the node's contents, etc.). All of this works perfectly. Now, I have to save it back out as an XML file. Apparently, this is near impossible in WPF.

At this point all I can think of doing is traversing the entire tree (from what I've read it may be necessary to first expand all nodes which is easily enough done) and then I can, one by one, read the nodes and write them to an XML file using xmlWriter.

So, unless somebody has a better idea, how can I in C# and WPF traverse an entire tree, getting the contents of each Node.name? Once I have the contents of each node I should be able to reconstitute the tree structure and write them out as an XML file.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
zetar
  • 1,225
  • 2
  • 20
  • 45
  • Normally one would bind the treeview to some underlying data model and save that data model. See e.g. [wpf treeview binding](https://stackoverflow.com/q/5295644/3744182) or [how to bind dynamic json into treeview wpf](https://stackoverflow.com/q/23812357/3744182) or https://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode. But I think you should be able to use `FindVisualChildren(treeView)` from [this question](https://stackoverflow.com/q/974598/3744182) to get all `TreeViewItem` children of a `TreeView`. – dbc Apr 30 '17 at 21:54
  • 1
    Typically, you will find that people comment on down-votes only if it seems necessary. Often, a close-vote suffices to indicate what is missing in a post (and you have two down-votes and two close-votes, so there seems to be parity there). Note that SO questions should be free of anything not _directly_ related to the question at hand. I've edited your question on your behalf to improve it so that it meets the community standards. Based on the evidence at hand, I would say your question earned down-votes by being too broad and not including a good [mcve]. See also [ask], including links at end – Peter Duniho May 01 '17 at 00:55

1 Answers1

-3

Try one of these solutions

IEnumerable<TreeNode> Collect(TreeNodeCollection nodes)
{
    foreach(TreeNode node in nodes)
    {
        yield return node;

        foreach (var child in Collect(node.Nodes))
            yield return child;
    }
}

Another solution

protected void TraverseNodes(TreeNodeCollection nodes, string action, int maxDepth = 2) 
    {
        foreach (TreeNode node in nodes)
        {
            if (node.ChildNodes.Count > 0 && node.Depth < maxDepth)
                TraverseNodes(node.ChildNodes, action, maxDepth);

            //do something!!!
            var x = node.Text;
            node.Checked = !node.Checked;
        }
    }
jdweng
  • 33,250
  • 2
  • 15
  • 20