日期:2014-05-18 浏览次数:20829 次
using System.Xml; using System.IO; private void AddXmlNode(TreeNodeCollection ATreeNodes, XmlNode AXmlNode) { string BMBH = string.Empty; string BMMC = string.Empty; string BMLX = string.Empty; XmlNode vXmlNode = AXmlNode.SelectSingleNode("BMBH"); if (vXmlNode != null) BMBH = vXmlNode.InnerText; vXmlNode = AXmlNode.SelectSingleNode("BMMC"); if (vXmlNode != null) BMMC = vXmlNode.InnerText; vXmlNode = AXmlNode.SelectSingleNode("BMBH"); if (vXmlNode != null) BMLX = vXmlNode.InnerText; TreeNode vTreeNode = ATreeNodes.Add(BMBH, BMMC); vTreeNode.Tag = BMLX; foreach (XmlNode vTemp in AXmlNode.SelectNodes("BM")) { AddXmlNode(vTreeNode.Nodes, vTemp); } } private void XmlToTree(string AFileName, TreeView ATreeView) { if (ATreeView == null) return; if (!File.Exists(AFileName)) return; // 文件不存在 XmlDocument vXmlDocument = new XmlDocument(); vXmlDocument.Load(AFileName); XmlElement vRoot = vXmlDocument.DocumentElement; foreach (XmlNode vXmlNode in vRoot.ChildNodes) { AddXmlNode(treeView1.Nodes, vXmlNode); } } private void button1_Click(object sender, EventArgs e) { XmlToTree(@"c:\temp\temp.xml", treeView1); }