treeview载如XML中的数据

treeview加载XML,其中,Name 为节点的Text,Formel 为节点的Name,Explain 为节点的Tag,并且要能实现增删改的操作。
XML文件:
XML code
 
   
<? xml version="1.0" encoding="utf-8" ?> < root > < BM > < Name > COD排放 </ Name > < Formel ></ Formel > < Explain ></ Explain > < BM > < Name > 排放公式一 </ Name > < Formel > a+b </ Formel > < Explain > 2 </ Explain > </ BM > < BM > < Name > 排放公式二 </ Name > < Formel > a+b-c </ Formel > < Explain > 2 </ Explain > </ BM > </ BM > < BM > < Name > SO2排放 </ Name > < Formel > a*(b+c) </ Formel > < Explain ></ Explain > < BM > < Name > 计算公式一 </ Name > < Formel > a*(b+c)-d </ Formel > < Explain > 2 </ Explain > </ BM > < BM > < Name > 计算公式二 </ Name > < Formel > a*(b-c)+d </ Formel > < Explain > 2 </ Explain > < BM > < Name > 计算公式三 </ Name > < Formel > a-b </ Formel > < Explain > 2 </ Explain > < BM > < Name > 计算公式四 </ Name > < Formel > a+b*c </ Formel > < Explain > 2 </ Explain > </ BM > </ BM > </ BM > </ BM > </ root >

C# code
 
   
private void populateTreeview() { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = " Open XML Document " ; dlg.Filter = " XML Files (*.xml)|*.xml " ; dlg.FileName = Application.StartupPath + " \\..\\..\\example.xml " ; if (dlg.ShowDialog() == DialogResult.OK) { try { // Just a good practice -- change the cursor to a // wait cursor while the nodes populate this .Cursor = Cursors.WaitCursor; // First, we'll load the Xml document XmlDocument xDoc = new XmlDocument(); xDoc.Load(dlg.FileName); // Now, clear out the treeview, // and add the first (root) node treeView1.Nodes.Clear(); treeView1.Nodes.Add( new TreeNode(xDoc.DocumentElement.Name)); TreeNode tNode = new TreeNode(); tNode = (TreeNode)treeView1.Nodes[ 0 ]; // We make a call to addTreeNode, // where we'll add all of our nodes addTreeNode(xDoc.DocumentElement, tNode); // Expand the treeview to show all nodes treeView1.ExpandAll(); } catch (XmlException xExc) // Exception is thrown is there is an error in the Xml { MessageBox.Show(xExc.Message); } catch (Exception ex) // General exception { MessageBox.Show(ex.Message); } finally { this .Cursor = Cursors.Default; // Change the cursor back } } } // This function is called recursively until all nodes are loaded private void addTreeNode(XmlNode xmlNode, TreeNode treeNode) { XmlNode xNode; TreeNode tNode; XmlNodeList xNodeList; if (xmlNode.HasChildNodes) // The current node has children { xNodeList = xmlNode.ChildNodes; for ( int x = 0 ; x <= xNodeList.Count - 1 ; x ++ ) // Loop through the child nodes { xNode = xmlNode.ChildNodes[x]; treeNode.Nodes.Add( new TreeNode(xNode.Name)); tNode = treeNode.Nodes[x]; addTreeNode(xNode, tNode); } } else // No children, so add the outer xml (trimming off whitespace) treeNode.Text = xmlNode.OuterXml.Trim(); } save private StreamWriter sr; public void exportToXml(TreeView tv, string filename) { sr = new StreamWriter(filename, false , System.Text.Encoding.UTF8); // Write the header sr.WriteLine( " <?xml version=\"1.0\" encoding=\"utf-8\" ?> " ); // Write our root node sr.WriteLine( " < " + treeView1.Nodes[ 0 ].Text + " > " ); foreach (TreeNode node in tv.Nodes) { saveNode(node.Nodes); } // Close the root node sr.WriteLine( " </ " + treeView1.Nodes[ 0 ].Text + " > " ); sr.Close(); } private void saveNode(TreeNodeCollection tnc) { foreach (TreeNode node in tnc) { // If we have child nodes, we'll write // a parent node, then iterrate through // the children if (node.Nodes.Count > 0 ) { sr.WriteLine( " < " + node.Text + " > " ); saveNode(node.Nodes); sr.WriteLine( " </ " + node.Text + " > " ); } else // No child nodes, so we just write the text sr.WriteLine(node.Text); } }

转载于:https://www.cnblogs.com/viki117/archive/2008/06/20/1226961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值