gpt4 book ai didi

c# - Linq to XML 查询

转载 作者:数据小太阳 更新时间:2023-10-29 01:50:03 24 4
gpt4 key购买 nike

假设我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer Name="Jason Voorhees" WeaponPurchased="Machette" SalePrice="499.90" />
<Customer Name="Michael Myers" WeaponPurchased="Kitchen Knife" SalePrice="96.75" />
</Customers>

使用 Linq 是否有可能做这样的事情:?

foreach customer in Customers select WeaponPurchased where Name equals "Jason Voorhees"

或:

foreach customer in Customers select customer
label1.Text += "Name: " + customer.Name + Environment.NewLine + "WeaponPurchased: " + customer.WeaponPurchased;

我以前在 MSDN 上见过这种类型的查询,但我收藏夹中的链接现在指向错误的页面,我仍在努力寻找这些特定的示例。非常感谢任何帮助,

谢谢

最佳答案

试试这个:

var doc = XDocument.Load(Path.Combine(path, "file.xml"));
var query = from c in doc.Descendants("Customer")
where c.Attributes("Name").Single().Value == "Jason Voorhees"
select c.Attributes("WeaponPurchased").Single().Value;

它将返回 IEnumerable<string>带有武器名称。

关于c# - Linq to XML 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6015673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com