Linq方法实现TreeViewToXml
#region Linq方法实现TreeViewToXml //保存 private void SaveTOXml() { XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes"); XDocument xml = new XDocument(dec); XElement root = new XElement("Tree"); foreach (TreeNode node in treeView1.Nodes) { XElement e = CreateElements(node); root.Add(e); } xml.Add(root); xml.Save(mf.DirXml + @"\Nav.xml"); } private XElement CreateElements(TreeNode node) { XElement root = CreateElement(node); foreach (TreeNode n in node.Nodes) { XElement e = CreateElements(n); root.Add(e); } return root; } private XElement CreateElement(TreeNode node) { if (node.Tag == null) { return new XElement("Site", new XAttribute("Name", node.Name), new XAttribute("Text", node.Text), new XAttribute("ImageIndex", node.ImageIndex), new XAttribute("SelectedImageIndex", node.SelectedImageIndex), new XAttribute("Link", "")); } else { return new XElement("Site", new XAttribute("Name", node.Name), new XAttribute("Text", node.Text), new XAttribute("ImageIndex", node.ImageIndex), new XAttribute("SelectedImageIndex", node.SelectedImageIndex), new XAttribute("Link", node.Tag)); } } #endregion