读书人

请帮帮忙!winform下用treeview绑定xm

发布时间: 2011-12-14 23:20:17 作者: rapoo

请各位高手帮帮忙!!!winform下用treeview绑定xml动态生成特定的树
我有一个xml文件如下:
<?xml version="1.0" encoding="utf-8"? >
<root >
<BM >
<BMBH >1 </BMBH >
<BMMC >广告部 </BMMC >
<BMLX >1 </BMLX >
<BM >
<BMBH >11 </BMBH >
<BMMC >策划组 </BMMC >
<BMLX >2 </BMLX >
</BM >
<BM >
<BMBH >12 </BMBH >
<BMMC >发行组 </BMMC >
<BMLX >2 </BMLX >
</BM >
</BM >
<BM >
<BMBH >2 </BMBH >
<BMMC >后勤部 </BMMC >
<BMLX >1 </BMLX >
<BM >
<BMBH >21 </BMBH >
<BMMC >采购组 </BMMC >
<BMLX >2 </BMLX >
</BM >
<BM >
<BMBH >22 </BMBH >
<BMMC >分配组 </BMMC >
<BMLX >2 </BMLX >
</BM >
</BM >
</root >

而要构造出的树是这样的:
--广告部
--策划组
--发行组
--后勤部
--采购组
--分配组

其中 <BMBH >和 <BMLX >要分别作为Name和Tag存入各自对应的节点。

请各位高手帮忙解答,最好有代码作为参考,谢谢!

[解决办法]
参考如下代码:

C# code
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);    } 

读书人网 >C#

热点推荐