为什么此 Linq to XML 查询不起作用

发布于 2024-09-18 18:07:17 字数 1237 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-09-25 18:07:17

编辑:嗯...我不知道 删除< /code> 扩展方法之前。

问题是,当您将元素转换为字符串时,它会连接所有后代文本节点......因此“Joe”元素的值实际上是“Joe the Yet”。

您只需要直接子文本节点。坦率地说,如果名称位于属性中而不是内容中会更容易,但它仍然应该可行...

此外,您正在直接在 BookParticipantsFirstName 元素code> 而不是 BookParticipant

这是可行的,尽管它不是很令人愉快:

xDocument.Descendants("BookParticipants")
         .Elements()
         .Where(x => x.Elements("FirstName")
                      .Nodes()
                      .OfType<XText>()
                      .Any(t => t.Value== "Joe"))
         .Remove();

如果您愿意,您也可以将第一位更改为

xDocument.Descendants("BookParticipant")
         .Where(...)

(同样,如果您可以使用字符串值的属性而不是元素中的内容,那就会让生活变得更轻松。)

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 under BookParticipants rather than BookParticipant.

This works, although it's not terribly pleasant:

xDocument.Descendants("BookParticipants")
         .Elements()
         .Where(x => x.Elements("FirstName")
                      .Nodes()
                      .OfType<XText>()
                      .Any(t => t.Value== "Joe"))
         .Remove();

You could change the first bit to

xDocument.Descendants("BookParticipant")
         .Where(...)

if you want, too.

(Again, if you can use attributes for string values rather than content in elements, that makes life easier.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文