您的位置:首页 > 其它

XML基本操作:创建增删改

2009-07-15 14:26 435 查看
using System;
using System.Text;
using System.Xml;
namespace XMLDOM
{
public class XmlOperation
{

private static void Main(string [] args)
{
XmlOperation xml=new XmlOperation();
Console.WriteLine ("1Create  2 Insert 3 Update 4 Delete 5 Display 6 Exit");
string c="";
do
{
c=Console.ReadLine();
switch(c)
{
case "1":xml.Create();break;
case "2":xml.Insert();break;
case "3":xml.Update();break;
case "4":xml.Delete();break;
case "5":xml.Display();break;
}
}while(c!="6");
}
private void Display()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Books.xml");
Console.WriteLine(xmlDoc.InnerXml);
Console.WriteLine("Operation Finished");

}
//创建文档
private void Create()
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "GB2312", null);
xmlDoc.AppendChild(dec);
//创建根节点
XmlElement root = xmlDoc.CreateElement("Books");
xmlDoc.AppendChild(root);
XmlNode book = xmlDoc.CreateElement("Book");
XmlElement title = xmlDoc.CreateElement("Title");
title.InnerText = "SQL Server";
book.AppendChild(title);
XmlElement isbn = xmlDoc.CreateElement("ISBN");
isbn.InnerText = "444444";
book.AppendChild(isbn);
XmlElement author = xmlDoc.CreateElement("Author");
author.InnerText = "jia";
book.AppendChild(author);
XmlElement price = xmlDoc.CreateElement("Price");
price.InnerText = "120";
price.SetAttribute("Unit", "aaaa");
book.AppendChild(price);
root.AppendChild(book);
xmlDoc.Save("Books.xml");
Console.WriteLine("Operation Finished");
}
/// <summary>
/// 插入节点
/// </summary>
private void Insert()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Books.xml");
XmlNode root = xmlDoc.SelectSingleNode("Books");
XmlElement book = xmlDoc.CreateElement("Book");
XmlElement title = xmlDoc.CreateElement("Title");
title.InnerText = "XML"; //XmlNode txt=xmlDoc.CreateTextNode("XML");
book.AppendChild(title);
XmlElement isbn = xmlDoc.CreateElement("ISBN");
isbn.InnerText = "333333";
book.AppendChild(isbn);
XmlElement author = xmlDoc.CreateElement("Author");
author.InnerText = "snow";
book.AppendChild(author);
XmlElement price = xmlDoc.CreateElement("Price");
price.InnerText = "120";
price.SetAttribute("Unit", "aaa");
book.AppendChild(price);
root.AppendChild(book);
xmlDoc.Save("Books.xml");
Console.WriteLine("Operation Finished");
}
/// <summary>
/// 更新节点
/// </summary>
private void Update()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Books.xml");
//"//Book[@Unit="$"]"
//获取Books节点的所有子节点
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
if (xe.Name == "Author")
{
xe.InnerText = "amandag";
}
if (xe.GetAttribute("Unit") == "aaa" )
{
xe.SetAttribute("Unit", "¥");
}
}
xmlDoc.Save("Books.xml");
Console.WriteLine("Operation Finished");
}
/// <summary>
/// 删除节点
/// </summary>
private void Delete()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Books.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
if(xe.Name == "Author")
{
xe.RemoveAll();
}
if(xe.GetAttribute("Unit") == "¥")
{
xe.RemoveAttribute("Unit");
}
}
xmlDoc.Save("Books.xml");
Console.WriteLine("Operation Finished");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐