'How to save treeview xml correctly when I edit it in winfroms?
Let's say. If I have this kind of xml file.
<Main>
<group name="R1">
<IsUser>Example</IsUser>
<IsUser>Example2</IsUser>
</group>
</Main>
I tried to render into a treeview and removed this part.
<IsUser>Example2</IsUser>
And save it with this code.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
ExportToXML(treeView, FileName);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
public void ExportToXML(TreeView tree, string filename)
{
XmlTextWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
XmlTextWriter.WriteStartDocument();
XmlTextWriter.WriteStartElement(treeViewReport.Nodes[0].Text);
foreach (TreeNode node in tree.Nodes)
{
SaveToXML(node.Nodes);
}
XmlTextWriter.WriteEndElement();
XmlTextWriter.Close();
}
private void SaveToXML(TreeNodeCollection menu)
{
foreach (TreeNode node in menu)
{
if (node.Nodes.Count > 0)
{
XmlTextWriter.WriteStartElement(node.Text);
SaveToXML(node.Nodes);
XmlTextWriter.WriteEndElement();
}
else
{
XmlTextWriter.WriteString(node.Text);
}
}
}
but It's saved like this.
<?xml version="1.0" encoding="utf-8"?><Main><group name="R1" ><IsUser >Example</IsUser ></group name="R1" ></Main>
How to save it corretly? Thank you!
-----Edit-----
It should be look like this.
<Main>
<group name="R1">
<IsUser>Example</IsUser>
</group>
</Main>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|