为什么此 Linq to XML 查询不起作用
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 子句删除元素?
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);
}
}
}
I am trying to remove the joe element using the where clause. But this is not working. is it possible to delete elements using the where clause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:嗯...我不知道
删除< /code> 扩展方法
之前。
问题是,当您将元素转换为字符串时,它会连接所有后代文本节点......因此“Joe”元素的值实际上是“Joe the Yet”。
您只需要直接子文本节点。坦率地说,如果名称位于属性中而不是内容中会更容易,但它仍然应该可行...
此外,您正在直接在
BookParticipantsFirstName
元素code> 而不是BookParticipant
。这是可行的,尽管它不是很令人愉快:
如果您愿意,您也可以将第一位更改为
。
(同样,如果您可以使用字符串值的属性而不是元素中的内容,那就会让生活变得更轻松。)
EDIT: Hmm... I wasn't aware of that
Remove
extension method before.The problem is that when you convert an element into a string, it concatenates all the descendant text nodes... so the value of the "Joe" element is actually "Joe the yet".
You only want direct child text nodes. Frankly it would be easier if the name was in an attribute rather than as content, but it should still be feasible...
Additionally, you're looking for
FirstName
elements directly underBookParticipants
rather thanBookParticipant
.This works, although it's not terribly pleasant:
You could change the first bit to
if you want, too.
(Again, if you can use attributes for string values rather than content in elements, that makes life easier.)