【发布时间】:2010-09-14 16:40:02
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
XDocument xDocument = new XDocument(
new XElement("BookParticipants",
new XElement("BookParticipant",
new XAttribute("type", "Author"),
new XElement("FirstName", "Joe", new XElement("name",
new XElement("noot", "the"),
new XElement("iscool", "yes"))),
new XElement("LastName", "Rattz")),
new XElement("BookParticipant",
new XAttribute("type", "Editor"),
new XElement("FirstName", "Ewan", new XElement("name",
new XElement("noot", "the"),
new XElement("iscool", "yes"))),
new XElement("LastName", "Buckingham"))));
xDocument.Descendants("BookParticipants").Where(x => (string)x.Element("FirstName") == "Joe").Remove();
Console.WriteLine(xDocument);
}
}
}
我正在尝试使用 where 子句删除 joe 元素。但这不起作用。是否可以使用 where 子句删除元素?
【问题讨论】:
-
“这不起作用”到底是什么意思?编译器错误?例外?出乎意料的结果?
-
当我运行代码时,Joe 元素仍然存在。它没有被删除。这里有什么想法吗?
标签: c# linq linq-to-xml