本文共 950 字,大约阅读时间需要 3 分钟。
//-----------------------------------------------------------// 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); } }}
转载地址:http://ltje.baihongyu.com/