using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
第一步;声明文档对象
//XmlDocument doc = new XmlDocument();
XmlDocument doc = new XmlDocument();
XmlDocument doc = new XmlDocument();
第二步;加载XML文件
//doc.Load("..//..//XMLFile1.xml");
doc.Load("../../XMLFile1.xml");
获取XML文件的根节点
XmlNode node = doc.DocumentElement;
//XmlElement element = doc.DocumentElement;
XmlElement element = doc.DocumentElement;
接收子节点集合
//XmlNodeList nodelist = element.ChildNodes;
XmlNodeList nodelist = element.ChildNodes;
MessageBox.Show(nodelist[0].Attributes["id"].Value);//第0个子节点属性“id”的值
//MessageBox.Show("书名称:"+nodelist[0].ChildNodes[0].ChildNodes[0].Value+" 价格:"+nodelist[0].ChildNodes[1].ChildNodes[0].Value);
XmlDocument doc = new XmlDocument();
doc.Load("..//..//XMLFile1.xml");
XmlElement element = doc.DocumentElement;
XmlNodeList nodelist = element.ChildNodes;
MessageBox.Show("书名称:"+nodelist[0].ChildNodes [0].ChildNodes [0].Value +" 价格:"+nodelist[0].ChildNodes[1].ChildNodes[0].Value );
}
private void button2_Click(object sender, EventArgs e)
{
//string name = textBox1.Text.Trim();
//XmlDocument doc = new XmlDocument();
//doc.Load("..//..//XMLFile1.xml");
//XmlElement element = doc.DocumentElement;
//int count = element.ChildNodes.Count;
//string price="";
//for (int i = 0; i < count;i++ )
//{
// if (element.ChildNodes[i].ChildNodes[0].ChildNodes[0].Value == name)
// {
// price = element.ChildNodes[i].ChildNodes[1].ChildNodes[0].Value;
// break;
// }
//}
//MessageBox.Show("name:"+name+"\n"+"price:"+price);
string price="";
string name = textBox1.Text.Trim();
XmlDocument doc = new XmlDocument();
doc.Load("..//..//XMLFile1.xml");
XmlElement element = doc.DocumentElement;
int count = element.ChildNodes.Count;
for (int i = 0; i < count; i++)
{
if (element.ChildNodes[i].ChildNodes[0].ChildNodes[0].Value == name)
{
price = element.ChildNodes[i].ChildNodes[1].ChildNodes[0].Value;
break;
}
}
MessageBox.Show("书名:"+name +" 价格:"+price);
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
//string name = textBox2.Text.Trim();
//string price = textBox3.Text.Trim();
//XmlDocument doc = new XmlDocument();
//doc.Load("..//..//XMLFile1.xml");
//XmlElement element = doc.DocumentElement;
//XmlNode nodebook = doc.CreateElement("book");//通过文档对象创建Node节点
//XmlAttribute attribute = doc.CreateAttribute("id");//创建属性节点
//attribute.Value = "3";//给属性节点赋值
//nodebook.Attributes.Append(attribute);//把属性节点添加到book节点中
//XmlNode nodename = doc.CreateElement("name");
//nodename.InnerText = name;//设置nodename里面的值
//nodebook.AppendChild(nodename);//将nodename添加到nodebook节点当中
//XmlNode nodeprice = doc.CreateElement("price");
//nodeprice.InnerText = price;//设置nodeprice里面的值
//nodebook.AppendChild(nodeprice);//将nodeprice添加到nodebook节点当中
//element.AppendChild(nodebook);//添加到更节点中
//doc.Save("..//..//XMLFile1.xml");//保存文件
//MessageBox.Show("添加成功");
string name = textBox2.Text.Trim();
string price = textBox3.Text.Trim();
XmlDocument doc = new XmlDocument();
doc.Load("..//..//XMLFile1.xml");
XmlElement element = doc.DocumentElement;
XmlNode nodebook = doc.CreateElement("book");
XmlAttribute attribute = doc.CreateAttribute("id");
attribute.Value = "4";
nodebook.Attributes.Append(attribute);
XmlNode nodename = doc.CreateElement("name");
nodename.InnerText = name;
nodebook.AppendChild(nodename);
XmlNode nodeprice = doc.CreateElement("price");
nodeprice.InnerText = price;
nodebook.AppendChild(nodeprice);
element.AppendChild(nodebook);
doc.Save("..//..//XMLFile1.xml");
MessageBox.Show("添加成功!!!");
MessageBox.Show("书名: "+name +"\n"+"价格:"+price );
}
private void button4_Click(object sender, EventArgs e)
{
//string name = textBox2.Text.Trim();
//string price = textBox3.Text.Trim();
//XmlDocument doc = new XmlDocument();
//doc.Load("..//..//XMLFile1.xml");
//XmlElement element = doc.DocumentElement;
//XmlNode node = doc.CreateElement("book");
//XmlAttribute attribute = doc.CreateAttribute("id");
//attribute.Value = "6";
//node.Attributes.Append(attribute);
//node.InnerXml = "<name>" + name + "</name><price>" + price + "</price>";
//element.AppendChild(node);
//doc.Save("..//..//XMLFile1.xml");
//Bind();
//MessageBox.Show("添加成功");
string name = textBox2.Text.Trim();
string price = textBox3.Text.Trim();
XmlDataDocument doc = new XmlDataDocument();
doc.Load("..//..//XMLFile1.xml");
XmlElement element = doc.DocumentElement;
XmlNode node = doc.CreateElement("book");
XmlAttribute attribute = doc.CreateAttribute("id");
attribute.Value = "7";
node.Attributes.Append(attribute);
node.InnerXml = "<name>" + name + "</name><price>" + price + "</price>";
element.AppendChild(node);
doc.Save("..//..//XMLFile1.xml");
Bind();
MessageBox.Show("添加成功");
}
private void button5_Click(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
DataSet ds = new DataSet();
ds.ReadXml("..//..//XMLFile1.xml");//相对路径读取方法 “..//”表示上级路径的意思
//ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "") + "XMLFile1.xml");//绝对路径读法(带盘符的都是绝对路径)
//MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug",""));//AppDomain 应用程序域
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button6_Click(object sender, EventArgs e)
{
//string name = textBox4.Text.Trim();
//XmlDocument doc = new XmlDocument();
//doc.Load("..//..//XMLFile1.xml");
//XmlElement element = doc.DocumentElement;
//XmlNode node = null;//声明以个全局性的节点
//int count = element.ChildNodes.Count;
//for (int i = 0; i < count; i++)
//{
// if (element.ChildNodes[i].ChildNodes[0].InnerText == name)
// {
// node = element.ChildNodes[i];//给全局性节点赋值
// break;
// }
//}
//element.RemoveChild(node);
//doc.Save("..//..//XMLFile1.xml");
//Bind();
//MessageBox.Show("删除成功");
string name = textBox4.Text.Trim();
XmlDocument doc = new XmlDocument();
doc.Load("..//..//XMLFile1.xml");
XmlElement element = doc.DocumentElement;
//XmlNodeList nodelist = null ;
int count = element.ChildNodes.Count;
for (int i = 0; i < count; i++)
{
XmlNode node = null;
if (element.ChildNodes[i].ChildNodes[0].ChildNodes[0].Value == name)
{
node = element.ChildNodes[i];
element.RemoveChild(node);
}
}
doc.Save("..//..//XMLFile1.xml");
Bind();
MessageBox.Show ("删除成功!!!");
}
}
}