Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
113 个问题
我正在尝试制作一个像“Microsoft XML记事本”这样的XML编辑器(treeview)。 因此,如果我在编辑器中编辑某些内容,它应该反映在xml文件中。
-----例-----
Note:此问题总结整理于:How to save treeview xml in winforms?
欢迎来到Microsoft问答,请参考以下代码示例将treeview xml保存在winform中。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string FileName { get; set; }
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open XML Document";
dlg.Filter = "XML Files (*.xml)|*.xml";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
FileName = dlg.FileName;
this.Cursor = Cursors.WaitCursor;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(dlg.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)treeView1.Nodes[0];
addTreeNode(xDoc.DocumentElement, tNode);
treeView1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
}
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes)
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
{
xNode = xmlNode.ChildNodes[x];
string nodetext = xNode.Name;
Console.WriteLine(nodetext);
if (!nodetext.Contains("text"))
{
if (xNode.Attributes.Count > 0)
{
for (int i = 0; i < xNode.Attributes.Count; i++)
{
nodetext +=" "+xNode.Attributes[i].Name + "=" + "\"" + xNode.Attributes[i].Value+ "\"";
}
}
}
treeNode.Nodes.Add(new TreeNode(nodetext));
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
else
{
string nodetext = xmlNode.Name + ": ";
Console.WriteLine(nodetext);
if (!nodetext.Contains("text"))
{
if (xmlNode.Attributes.Count > 0)
{
for (int i = 0; i < xmlNode.Attributes.Count; i++)
{
nodetext += xmlNode.Attributes[i].Name + "=" + xmlNode.Attributes[i].Value;
}
}
}
else
{
nodetext = xmlNode.InnerText;
}
treeNode.Text = nodetext;
}
}
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
treeView1.LabelEdit = true;
treeView1.SelectedNode.BeginEdit();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
exportToXml(treeView1, FileName);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
//保存
StreamWriter sr = null;
public void exportToXml(TreeView tv, string filename)
{
sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
sr.WriteLine("<" + tv.Nodes[0].Text + ">");
foreach (TreeNode node in tv.Nodes)
{
saveNode(node.Nodes);
}
//Close the root node
sr.WriteLine("</" + tv.Nodes[0].Text + ">");
sr.Close();
var lines = File.ReadAllLines(filename).Where(line => !String.IsNullOrWhiteSpace(line));
File.WriteAllLines(filename, lines);
}
private void saveNode(TreeNodeCollection tnc)
{
foreach (TreeNode node in tnc)
{
if (node.Nodes.Count > 0)
{
if(node.Text.Contains("="))
{
sr.Write("<" + node.Text + ">");
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text.Split(' ')[0] + ">");
}
else
{
sr.WriteLine("");
sr.Write("<" + node.Text + ">");
saveNode(node.Nodes);
sr.WriteLine("</" + node.Text + ">");
}
}
else
sr.Write(node.Text);
}
}
}
测试结果:
注意:编辑节点后,您可以按将其保存到xml文件。ctrl+s
如果答案是正确的解决方案,请单击“接受答案”并投赞成票。如果您对此答案有其他疑问,请点击“评论”。
注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。