Linq to XML函数构造

函数构造是指通过单个语句构建XML树的能力.我们已经体验了如何使用XElement和XAttribute类手工创建XML树,也可以通过其他XElement和XAttribute对象传递给XElement类的构造函数来创建子元素和属性,也就是说,LINQ查询的结果可以用来创建XML树,下面就来看看一个演示:

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

using System;
using System.Xml.Linq;
using System.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)),
               
new XElement("Person",
                   
new XElement("Name", "Li Si"),
                   
new XElement("Sex", ""),
                   
new XElement("Age", 12))
                    ));

           
//通过LINQ查询将xel的元素添加至新的XElement中
            XElement newXel = new XElement(new XElement("NewRoot",
               
new XElement("Persons",
                    xel.Elements().Where(u
=> u.Name == "Person").Select(u => u)
                    )));

            Console.WriteLine(newXel);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值