using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace HashTableText
{
public partial class FrmTreeView : Form
{
private static int count = 1;
public FrmTreeView()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Clear();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Student.xml");
XmlNode xmlNode = xmlDoc.DocumentElement;
foreach (XmlNode node in xmlNode.ChildNodes)
{
TreeNode treeNode;
if (node.Attributes.Count != 0)
{
treeNode = this.treeView1.Nodes.Add(node.Attributes[0].Value);
foreach (XmlNode subNode in node.ChildNodes)
{
treeNode.Nodes.Add(subNode.InnerText);
}
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (this.treeView1.SelectedNode.Level == 1) return;
TreeNode treeNode = this.treeView1.SelectedNode;
this.textBox1.Text = treeNode.Nodes[0].Text;
this.textBox2.Text = treeNode.Nodes[1].Text;
this.textBox3.Text = treeNode.Nodes[2].Text;
this.textBox4.Text = treeNode.Text;
}
private void button2_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = getXmlDoc();
XmlElement Student = xmlDoc.CreateElement("Student");
XmlAttribute id = xmlDoc.CreateAttribute("ID");
id.Value = this.textBox4.Text.Trim();
Student.SetAttributeNode(id);
xmlDoc.DocumentElement.AppendChild(Student);
XmlElement Name = xmlDoc.CreateElement("Name");
Name.InnerText = this.textBox1.Text.Trim();
xmlDoc.DocumentElement.LastChild.AppendChild(Name);
XmlElement Age = xmlDoc.CreateElement("Age");
Age.InnerText = this.textBox2.Text.Trim();
xmlDoc.DocumentElement.LastChild.AppendChild(Age);
XmlElement Hobby = xmlDoc.CreateElement("Hobby");
Hobby.InnerText = this.textBox3.Text.Trim();
xmlDoc.DocumentElement.LastChild.AppendChild(Hobby);
xmlDoc.Save(@"Student.xml");
}
private void button3_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = getXmlDoc();
//XmlNode xmlNode = xmlDoc.SelectSingleNode("/Root/Student[@ID='"+this.textBox4.Text.Trim()+"']");
XmlNodeList MyNodeList = xmlDoc.SelectSingleNode("Root").ChildNodes;
foreach (XmlNode xmlNode in MyNodeList)
{
if (xmlNode.Attributes["ID"].Value == this.textBox4.Text.Trim())
{
XmlNodeList InfoPageNodeList = xmlNode.ChildNodes;
foreach (XmlNode subNode in InfoPageNodeList)
{
subNode.InnerText = this.textBox1.Text.Trim();
subNode.NextSibling.InnerText = this.textBox2.Text.Trim();
subNode.NextSibling.NextSibling.InnerText = this.textBox3.Text.Trim();
break;
}
}
}
xmlDoc.Save(@"Student.xml");
}
private void button4_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = getXmlDoc();
XmlNodeList MyNodeList = xmlDoc.SelectSingleNode("Root").ChildNodes;
foreach (XmlNode xmlNode in MyNodeList)
{
if (xmlNode.Attributes["ID"].Value == this.textBox4.Text.Trim())
{
xmlNode.RemoveAll();
xmlNode.ParentNode.RemoveChild(xmlNode);
break;
}
}
xmlDoc.Save(@"Student.xml");
}
private XmlDocument getXmlDoc()
{
XmlDocument doc = new XmlDocument();
doc.Load("Student.xml");
return doc;
}
}
}