Linq to Xml 增删查改

LINQ to XML一个重要的特性是能够方便地修改xml树,如添加、删除、更新和复制xml文档的内容。

I.插入

使用XNode类的插入方法可以方便地向xml树添加内容:

2010-05-05_162103

在下面的示例中,使用AddAfterSelf方法向现有xml中添加一个新节点:

	public static void AddAfterSelf()
        {
            XElement root = XElement.Parse(@"

                <Categories>

                  <Category>

                    <CategoryID>1</CategoryID>

                    <CategoryName>Beverages</CategoryName>

                    <Description>Soft drinks, coffees, teas, beers, and ales</Description>

                  </Category>

                </Categories>

            ");
            XElement xele = root.Element("Category").Element("CategoryName");
            xele.AddAfterSelf(new XElement("AddDate", DateTime.Now));
            //保存
            root.Save("test.xml");
        }
运行该示例将会得到一个xml文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<Categories>
  <Category>
    <CategoryID>1</CategoryID>
    <CategoryName>Beverages</CategoryName>
    <AddDate>2013-07-22T21:51:42.1550551+08:00</AddDate>
    <Description>Soft drinks, coffees, teas, beers, and ales</Description>
  </Category>
</Categories>
当需要添加一个元素到指定节点之前时,可以使用AddBeforeSelf方法。

II.更新

在LINQ to XML中更新xml内容可以使用以下几种方法:

2010-05-05_162242

在下面的示例中使用了ReplaceWith与SetElementValue方法对xml进行了更新操作:

	public static void Update()
        {
            XElement root = XElement.Parse(@"
                                   <Categories>
                                      <Category>
                                        <CategoryID>1</CategoryID>
                                        <CategoryName>Beverages</CategoryName>
                                        <Description>Soft drinks, coffees, teas, beers, and ales</Description>
                                      </Category>
                                    </Categories>
                                  ");
            root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID", "2"));
            root.Element("Category").SetElementValue("CategoryName", "test data");
            root.Save("test.xml");
        }
运行该示例将会得到一个xml文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<Categories>
  <Category>
    <ID>2</ID>
    <CategoryName>test data</CategoryName>
    <Description>Soft drinks, coffees, teas, beers, and ales</Description>
  </Category>
</Categories>

III.删除

可以使用Remove(XElement)与RemoveAll方法来删除xml。

在下面的示例中,使用了RemoveAll方法:

	public static void Remove()
        {
            string path = @"test.xml";
            XElement root = XElement.Parse(@"
                                  <Categories>

                                    <Category>

                                      <CategoryID>1</CategoryID>

                                      <CategoryName>Beverages</CategoryName>

                                      <Description>Soft drinks, coffees, teas, beers, and ales</Description>

                                    </Category>

                                  </Categories>

                                ");
            root.RemoveAll();
            root.Save(path);
        }
运行该示例将会得到一个xml文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<Categories />
在下面的示例中,使用了Remove方法删除了xml的Description元素:

	public static void Remove()
        {
            XElement root = XElement.Parse(@"
                                <Categories>
                                  <Category>
                                    <CategoryID>1</CategoryID>
                                    <CategoryName>Beverages</CategoryName>
                                    <Description>Soft drinks, coffees, teas, beers, and ales</Description>
                                  </Category>
                                </Categories>
                                ");
            root.Element("Category").Element("Description").Remove();
            root.Save("test.xml");
        }
运行该示例将会得到一个xml文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<Categories>
  <Category>
    <CategoryID>1</CategoryID>
    <CategoryName>Beverages</CategoryName>
  </Category>
</Categories>



处理属性

I.添加

LINQ to XML添加属性与添加元素师类似的,可以使用构造函数或者Add方法来添加属性:

	public static void AddAttribute()
        {
            XElement root = new XElement("Categories",
                new XElement("Category",
                    new XAttribute("CategoryID", "1"),
                    new XElement("CategoryName", "Beverages"),
                    new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
                )
            );
            root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString()));
            root.Save("test.xml");
        }
运行该示例将会得到一个xml文件,其内容为:

<?xml version="1.0" encoding="utf-8"?>
<Categories>
  <Category CategoryID="1" AddDate="2013/7/22">
    <CategoryName>Beverages</CategoryName>
    <Description>Soft drinks, coffees, teas, beers, and ales</Description>
  </Category>
</Categories>

II.检索

检索属性可以使用Attribute(name)方法:

	public static void SelectAttribute()
        {
            XElement root = new XElement("Categories",
                new XElement("Category",
                    new XAttribute("CategoryID", "1"),
                    new XElement("CategoryName", "Beverages"),
                    new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
                )
            );
            XAttribute xattr = root.Element("Category").Attribute("CategoryID");
            Console.WriteLine(xattr.Name);
            Console.WriteLine(xattr.Value);
        }

上述代码的运行结果为:

CategoryID

1



















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值