使用Linq to XML操作XML的元素

Linq to XML可以方便地修改XML树,如添加,删除,更新和复制XML文档的内容其中包括:
1.插入:使用XNode类中的某一种添加方法可以很容易地向一个XML树添加内容.(AddAfterSelf,AddBeforeSelf)
2.更新:使用LINQ to XML更新XML相当简单.(ReplaceWith)
3.删除:使用LINQ to XML删除XML只要导航到要删除的内容调用删除方法即可.(Remove,RemoveAll)

(1)插入元素

using System;
using System.Xml.Linq;

namespace ConsoleApplication1
{
   
class Program  
    {
       
static void Main(string[] args)
        {
           
//保存
            XElement xel = new XElement(new XElement("Root",
               
new XElement("Person",
                   
new XElement("Name","Huang Cong"))));

           
//在Hang Cong之前插入一个元素
            xel.Element("Person").Element("Name").AddBeforeSelf(new XElement("Sex", ""));

           
//在Hang Cong之后插入一个元素
            xel.Element("Person").Element("Name").AddAfterSelf(new XElement("Age", 22));

            Console.WriteLine(xel);
        }
    }
}

 

(2)更新元素

using System;
using System.Xml.Linq;

namespace ConsoleApplication1
{
   
class Program  
    {
       
static void Main(string[] args)
        {
           
//保存
            XElement xel = new XElement(new XElement("Root",
               
new XElement("Person",
                   
new XElement("Name", "Huang Cong"),
                   
new XElement("Sex", ""),
                   
new XElement("Age", 22))));

           
//更新某个元素
            xel.Element("Person").Element("Name").ReplaceWith(new XElement("Name","Li Si"));
            Console.WriteLine(xel);

            Console.WriteLine(
"--------------------------");
           
           
//更新一个元素以及子节点
            xel.Element("Person").ReplaceWith(new XElement("Person",
                   
new XElement("Name", "Zhang San"),
                   
new XElement("Sex", ""),
                   
new XElement("Age", 23)));
            Console.WriteLine(xel);
        }
    }
}

 

(3)删除元素

//-----------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 ,黄聪 , Ltd .
//-----------------------------------------------------------

using System;
using System.Xml.Linq;

namespace ConsoleApplication1
{
   
class Program  
    {
       
static void Main(string[] args)
        {
           
//保存
            XElement xel = new XElement(new XElement("Root",
               
new XElement("Person",
                   
new XElement("Name", "Huang Cong"),
                   
new XElement("Sex", ""),
                   
new XElement("Age", 22))));

           
//从父节点删除该元素,包括该元素下的所有子节点
            xel.Element("Person").Element("Sex").Remove();
            Console.WriteLine(xel);

            Console.WriteLine(
"--------------------------");

           
//删除某个元素下的所有子节点
            xel.Element("Person").RemoveAll();

            Console.WriteLine(xel);
        }
    }
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值