C# 如何使用 LINQ-to-XML 选择特定节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/594231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:36:47  来源:igfitidea点击:

How to select a specific node with LINQ-to-XML

c#linq-to-xml

提问by Edward Tanguay

I can select the first customer node and change its company name with the code below.

我可以选择第一个客户节点并使用以下代码更改其公司名称。

But how do I select customer node where ID=2?

但是如何选择 ID=2 的客户节点?

    XDocument xmldoc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XComment("These are all the customers transfered from the database."),
        new XElement("Customers",
            new XElement("Customer",
                new XAttribute("ID", 1),
                new XElement("FullName", "Jim Tester"),
                new XElement("Title", "Developer"),
                new XElement("Company", "Apple Inc.")
                ),
            new XElement("Customer",
                new XAttribute("ID", 2),
                new XElement("FullName", "John Testly"),
                new XElement("Title", "Tester"),
                new XElement("Company", "Google")
                )
            )
        );

    XElement elementToChange = xmldoc.Element("Customers").Element("Customer").Element("Company");
    elementToChange.ReplaceWith(new XElement("Company", "new company value..."));

ANSWER:

回答:

Thanks guys, for the record, here is the exact syntax to search out the company element in the customer-with-id-2 element, and then change only the value of the company element:

谢谢各位,为了记录,这里是在 customer-with-id-2 元素中搜索 company 元素的确切语法,然后只更改 company 元素的值:

XElement elementToChange = xmldoc.Element("Customers")
    .Elements("Customer")
    .Single(x => (int)x.Attribute("ID") == 2)
    .Element("Company");
elementToChange.ReplaceWith(
    new XElement("Company", "new company value...")
    );

ANSWER WITH METHOD SYNTAX:

用方法语法回答:

Just figured it out in method syntax as well:

刚刚在方法语法中也弄清楚了:

XElement elementToChange = (from c in xmldoc.Element("Customers")
                                .Elements("Customer")
                            where (int)c.Attribute("ID") == 3
                            select c).Single().Element("Company");

采纳答案by Mehrdad Afshari

Assuming the ID is unique:

假设 ID 是唯一的:

var result = xmldoc.Element("Customers")
                   .Elements("Customer")
                   .Single(x => (int?)x.Attribute("ID") == 2);

You could also use First, FirstOrDefault, SingleOrDefaultor Where, instead of Singlefor different circumstances.

您也可以使用First, FirstOrDefault,SingleOrDefaultWhere, 而不是Single用于不同的情况。

回答by Nick

I'd use something like:

我会使用类似的东西:

dim customer = (from c in xmldoc...<Customer> 
                where c.<ID>.Value=22 
                select c).SingleOrDefault 

Edit:

编辑:

missed the c# tag, sorry......the example is in VB.NET

错过了 c# 标签,抱歉……示例在 VB.NET 中