XML基本操作:创建增删改

Code
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");
    }
}
}

转载于:https://www.cnblogs.com/hubcarl/archive/2009/07/15/1524118.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值