我需要请教一个关于C#Windows中如何导出XML文档的问题。
需要在C#中把treeview树结构中的内容导出存为xml文档形式,我应该怎么编写。
private void button4_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
{
textBox5.Text = saveFileDialog1.FileName;
}
string path = textBox5.Text;
XmlDocument doc = new XmlDocument();
XmlTextWriter tw = new XmlTextWriter(path, null);
tw.Formatting = Formatting.Indented;
doc.LoadXml("<Menu></Menu>");
XmlElement root = doc.DocumentElement;
doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"), root);
TransTreeSav(treeView1.Nodes, root);
doc.Save(path);
MessageBox.Show("保存成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void TransTreeSav(TreeNodeCollection nodes, XmlElement ParXmlnode)
{
XmlDocument doc = ParXmlnode.OwnerDocument;
XmlNode root = doc.DocumentElement;;
foreach (TreeNode node in nodes)
{
XmlElement xmlnode = doc.CreateElement(node.Text); //这句代码说有问题(名称中不能包含“、”字符(十六进制值 0x3001)。)
XmlAttribute attr = doc.CreateAttribute("Title");
ParXmlnode.AppendChild(xmlnode);
if (node.Nodes.Count > 0)
{
&n