Question: How to replace section of XML file based off a TreeView?
Detail:
I have an XML file, which has two child nodes, one containing a flat structure of nodes, and one containing a hierachal representation of the nodes to mirror the TreeView structure.
I have taken this (potentially unothodox) approach as I want to be able to manipulate the TreeView (add/delete/move nodes, drag/drop etc) but keep the flat section intact (which holds a lot more data), and scenarios where nodes are not needed to be shown in the TreeView (e.g. CCC )
What I am struggling with is finding the best way to generate the xml based off the TreeView and transfer this back into the source file.
XML file looks like:
<?xml version="1.0" encoding="utf-8"?>
<TreeView Name="Test" >
<Flat>
<Node Name="AAA" />
<Node Name="BBB" />
<Node Name="CCC" />
<Node Name="DDD" />
<Node Name="EEE" />
</Flat>
<Tree>
<Node Name="DDD">
<Nodes />
</Node>
<Node Name="EEE" >
<Nodes>
<Node Name="BBB">
<Nodes />
</Node>
</Nodes>
</Node>
<Node Name="AAA" >
<Nodes />
</Node>
</Tree>
</TreeView>
My code for loading and reading the xml file recursively is shown below, all appears to work fine, and the TreeView is built off the hierachal section of the XML file. All good so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace TreeViewTest
{
public partial class Form1 : Form
{
string _File = @"C:\Data\Test.xml";
XmlDocument _XML = new XmlDocument();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Tree Test";
_LoadTree();
}
private void _LoadTree()
{
_XML.Load(_File);
foreach (XmlNode _XMLParent in _XML.DocumentElement.SelectNodes("/TreeViee/Tree"))
{
foreach (XmlNode _XMLChild in _XMLParent.ChildNodes)
{
TreeNode _TreeNode = new TreeNode();
_TreeNode.Text = _XMLChild.Attributes["Name"].InnerText;
_TreeNode.Name = _XMLChild.Attributes["Name"].InnerText;
_TreeNode.Tag = _XMLChild.Attributes["Name"].InnerText;
_tvTest.Nodes.Add(_TreeNode);
_LoadTreeRecursive(_XMLChild, _TreeNode);
}
}
}
private void _LoadTreeRecursive(XmlNode XMLNode, TreeNode TreeNode)
{
foreach (XmlNode _XMLParent in XMLNode.ChildNodes)
{
foreach (XmlNode _XMLChild in _XMLParent.ChildNodes)
{
TreeNode _TreeNode = new TreeNode();
_TreeNode.Text = _XMLChild.Attributes["Name"].InnerText;
_TreeNode.Name = _XMLChild.Attributes["Name"].InnerText;
_TreeNode.Tag = _XMLChild.Attributes["Name"].InnerText;
TreeNode.Nodes.Add(_TreeNode);
_LoadTreeRecursive(_XMLChild, _TreeNode);
}
}
}
private void _btnTest_Click(object sender, EventArgs e)
{
_SaveTree(_tvTest);
}
private void _SaveTree(TreeView _TreeView)
{
foreach (TreeNode _TreeNode in _TreeView.Nodes)
{
Console.WriteLine(_TreeNode.Text);
_SaveTreeRecursive(_TreeNode);
}
}
private void _SaveTreeRecursive(TreeNode TreeNode)
{
foreach (TreeNode _TreeNode in TreeNode.Nodes)
{
Console.WriteLine(_TreeNode.Text);
_SaveTreeRecursive(_TreeNode);
}
}
}
}
I can generate an XML file off the tree, e.g. using a StringWriter, and I can remove the "Tree" child nodes via LINQ, e.g.
XElement _Xel = XElement.Load(_File);
var _Remove = _XML.Elements("Tree")
_Remove.Remove;
What I am struggling with is a way to remove all the child nodes of the "Tree" node, and then add in the heirachal structure based off the TreeView.
So, how can I go about replacing the "Tree" section of the XML file based on my methods "_SaveTree" and "_SaveTreeRecursive" ?
Open to suggestions, been tearing my hair out a bit with this one, so I'm sure I'm just missing something obvious.
No comments:
Post a Comment