LINQ:根据属性值从 XML 中删除元素?

发布于 2024-10-17 09:09:35 字数 1273 浏览 0 评论 0原文

如何根据匹配的属性值删除 xml 中的任何元素?

这是我的 XML:

<Projects>
    <Project serverUrl="tcp://xyz1:xxx/sdfsdf.rem" projectName="project1" />
    <Project serverUrl="tcp://xyz2:xxx/sdfsdf.rem" projectName="project2" />
    <Project serverUrl="tcp://xyz3:xxx/sdfsdf.rem" projectName="project3" />
    <Project serverUrl="tcp://xyz4:xxx/sdfsdf.rem" projectName="project4" />
    <Project serverUrl="tcp://xyz5:xxx/sdfsdf.rem" projectName="project5" />
    <Project serverUrl="tcp://xyz6:xxx/sdfsdf.rem" projectName="project6" />
   </Projects>

我正在使用以下 LINQ 查询:

var remove = from elemet in xdoc.Elements("Projects").Elements("Project")
                                 where elemet.Attribute("projectName").Value == "project1"
                                 select elemet.Parent.Remove();

我在 select as 上遇到编译时错误:

select中表达式的类型 条款不正确

编辑答案: 这个对我有用。谢谢大家

var xElement = (from elemet in xdoc.Elements("Projects").Elements("Project")
                            where elemet.Attribute("projectName").Value == foundProject
                            select elemet);
            xElement.Remove();

How can I remove any element in xml based on matched attribute value?

Here is my XML :

<Projects>
    <Project serverUrl="tcp://xyz1:xxx/sdfsdf.rem" projectName="project1" />
    <Project serverUrl="tcp://xyz2:xxx/sdfsdf.rem" projectName="project2" />
    <Project serverUrl="tcp://xyz3:xxx/sdfsdf.rem" projectName="project3" />
    <Project serverUrl="tcp://xyz4:xxx/sdfsdf.rem" projectName="project4" />
    <Project serverUrl="tcp://xyz5:xxx/sdfsdf.rem" projectName="project5" />
    <Project serverUrl="tcp://xyz6:xxx/sdfsdf.rem" projectName="project6" />
   </Projects>

I am using the following LINQ query:

var remove = from elemet in xdoc.Elements("Projects").Elements("Project")
                                 where elemet.Attribute("projectName").Value == "project1"
                                 select elemet.Parent.Remove();

I am getting compile time error on select as :

The type of expression in select
clause is Incorrect

EDIT ANSWER:
this one works for me. Thanks All

var xElement = (from elemet in xdoc.Elements("Projects").Elements("Project")
                            where elemet.Attribute("projectName").Value == foundProject
                            select elemet);
            xElement.Remove();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

铜锣湾横着走 2024-10-24 09:09:35

Remove 是一个 (void) 方法调用,而不是一个可以返回值的函数。您可能想要这样的东西:

var elementsToRemove = from elemet in xdoc.Elements("Projects").Elements("Project")
                       where elemet.Attribute("projectName").Value == "project1"
                       select elemet;

foreach (var e in elementsToRemove)
    e.Remove();

LINQ 是一种查询语言,它(主要)用于返回某些内容。对这些元素执行操作通常是一个单独的步骤。

Remove is a (void) method call, not a function that can return a value. You probably want something like this:

var elementsToRemove = from elemet in xdoc.Elements("Projects").Elements("Project")
                       where elemet.Attribute("projectName").Value == "project1"
                       select elemet;

foreach (var e in elementsToRemove)
    e.Remove();

LINQ is a query language, it's (mainly) used to return something. Performing actions on these elements is usually a separate step.

林空鹿饮溪 2024-10-24 09:09:35

你可以使用

xdoc.Elements("Projects").Elements("Project").Where(
                    elemet => elemet.Attribute("projectName").Value == "project1")
.ToList().ForEach(i => i.Remove());

(from elemet in xdoc.Elements("Projects").Elements("Project")
                          where elemet.Attribute("projectName").Value == "project1"
                          select elemet).ToList().ForEach(i => i.Remove());

You could use

xdoc.Elements("Projects").Elements("Project").Where(
                    elemet => elemet.Attribute("projectName").Value == "project1")
.ToList().ForEach(i => i.Remove());

or

(from elemet in xdoc.Elements("Projects").Elements("Project")
                          where elemet.Attribute("projectName").Value == "project1"
                          select elemet).ToList().ForEach(i => i.Remove());
难理解 2024-10-24 09:09:35

您可以使用以下代码片段:

xdoc.XPathSelectElement("Projects/Project[@projectName = 'project1']").Remove();

You can use the following code snippet:

xdoc.XPathSelectElement("Projects/Project[@projectName = 'project1']").Remove();
喜你已久 2024-10-24 09:09:35

Remove() 是您在 XNode 上调用的方法。您的查询试图选择没有任何意义的方法。

您真正想要做的是选择要删除的项目,然后对所选项目调用Remove() 方法。您可以在这里找到一个示例:
XNode.Remove方法

Remove() is a method you call on an XNode. Your query is trying to select the method which doesn't make any sense.

What you actually want to do is select the item you wish to remove, then call the Remove() method on the selected item. You can find an example here:
XNode.Remove Method

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