資源簡介
實現讀取xml文件并在treeview上顯示并添加修改刪除節點

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
using?System.xml;
namespace?xmlTreeAPP
{
????public?partial?class?Form1?:?Form
????{
????????xmlDocument?doc?=?new?xmlDocument();
????????public?string?str1?=?““;
????????public?string?strnode?=?““;
????????public?string?filename?=?““;
????????public?string?attributeName?=?““;
????????public?string?attributeVale?=?““;
????????public?int?i?=?0;
????????private?Point?Position?=?new?Point(0?0);
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????//加載文件
????????private?void?button_load_Click(object?sender?EventArgs?e)
????????{
????????????//string?xmlfileaddr=““;
????????????OpenFileDialog?dlg?=?new?OpenFileDialog();
????????????dlg.InitialDirectory?=?“D:\\“;
????????????dlg.Filter?=?“xml?files(*.xml)|*.xml“;
????????????dlg.FilterIndex?=?2;
????????????dlg.RestoreDirectory?=?true;
????????????if(dlg.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????filename?=?dlg.FileName;
????????????}
????????????LoadxmlFile(filename);
????????}
????????public?void?LoadxmlFile(string?fileName)
????????{
????????????doc.Load(fileName);
????????????xmlNode?xmlRoot?=?doc.DocumentElement;
????????????TreeNode?treeRoot?=?new?TreeNode(xmlRoot.Name);
????????????trView.Nodes.Add(treeRoot);
????????????//textBox1.Text?=?trView.SelectedNode.Text;
????????????LoadToTreeView(treeRoot?xmlRoot);
????????????trView.ExpandAll();
????????}
????????public?void?LoadToTreeView(TreeNode?treenodexmlNode?xmlNode)
????????{
????????????foreach(xmlNode?leaf?in?xmlNode.ChildNodes)
????????????{
????????????????TreeNode?treeleaf?=?null;
????????????????string?str;
????????????????str?=?leaf.Name;
????????????????if?(str?==?“#text“)
????????????????{
????????????????????treeleaf?=?new?TreeNode(leaf.InnerText);
????????????????????treenode.Nodes.Add(treeleaf);
????????????????}
????????????????else
????????????????{
????????????????????treeleaf?=?new?TreeNode(str);
????????????????????treenode.Nodes.Add(treeleaf);
????????????????????LoadToTreeView(treeleaf?leaf);
????????????????}
????????????}
????????}
/*
????????private?void?button_save_Click(object?sender?EventArgs?e)
????????{
????????????string?str?=?““;
????????????SaveFileDialog?dlg?=?new?SaveFileDialog();
????????????dlg.InitialDirectory?=?“c:\\“;
????????????dlg.Filter?=?“xml?files(*.xml)|*.xml“;
????????????dlg.FilterIndex?=?2;
????????????dlg.RestoreDirectory?=?true;
????????????if?(dlg.ShowDialog()?==?DialogResult.OK)
????????????{
????????????????str?=?dlg.FileName;
????????????}
????????????doc.Save(str);
????????}
*/
????????//刪除
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????textBox1.Text?=?trView.SelectedNode.Text;????//選中節點的文
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????187??2014-12-19?21:22??xm
?????文件????????476??2018-01-22?11:04??xm
?????文件??????15360??2018-01-23?08:56??xm
?????文件????????187??2014-12-19?21:22??xm
?????文件??????28160??2018-01-23?08:56??xm
?????文件??????22696??2018-01-23?17:38??xm
?????文件????????187??2014-12-19?21:22??xm
?????文件????????490??2010-03-17?22:39??xm
?????文件???????9634??2018-01-23?18:06??xm
?????文件??????10906??2018-01-22?11:16??xm
?????文件???????6192??2018-01-22?11:16??xm
?????文件???????1452??2018-01-20?20:03??xm
?????文件???????7112??2018-01-22?09:04??xm
?????文件??????????0??2014-12-19?21:22??xm
?????文件??????????0??2014-12-19?21:22??xm
?????文件??????????0??2014-12-19?21:22??xm
?????文件?????????42??2018-01-21?20:36??xm
?????文件???????2934??2018-01-23?17:38??xm
?????文件???????1072??2018-01-22?11:16??xm
?????文件???????2210??2018-01-20?16:37??xm
?????文件??????15360??2018-01-23?08:56??xm
?????文件????????180??2018-01-22?11:16??xm
?????文件??????28160??2018-01-23?08:56??xm
?????文件????????180??2018-01-21?20:36??xm
?????文件????????522??2014-12-19?21:22??xm
?????文件???????1344??2014-12-19?21:22??xm
?????文件???????2872??2014-12-19?21:22??xm
?????文件???????5612??2014-12-19?21:22??xm
?????文件???????1097??2014-12-19?21:22??xm
?????文件????????249??2014-12-19?21:22??xm
............此處省略13個文件信息
- 上一篇:Operator Mono字體
- 下一篇:圖中任意兩點間所有路徑高效算法
評論
共有 條評論