Linq to XML 查询
假设我有一个如下所示的 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>
Is it possible, with Linq, to do things like this:?
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 上见过这种类型的查询,但我收藏夹中的链接现在指向错误的页面,我仍在尝试查找这些特定示例。非常感谢任何帮助,
谢谢
Let's just say I have an XML file that looks like this:
<?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>
Is it possible, with Linq, to do something like this:?
foreach customer in Customers select WeaponPurchased where Name equals "Jason Voorhees"
or:
foreach customer in Customers select customer
label1.Text += "Name: " + customer.Name + Environment.NewLine + "WeaponPurchased: " + customer.WeaponPurchased;
I've seen this type of query before on MSDN, but the links in my favorites lead to the wrong page now, and I'm still trying to find these particular examples. Any help is much appreciated,
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
它将返回带有武器名称的
IEnumerable
。Try this:
It will return
IEnumerable<string>
with names of weapons.尝试使用此查询
或者您可以尝试创建在 xml 中定义的类并反序列化它们。
Try to use this query
Or you can try to create classes which a defined in xml and deserialize them.
尝试这个
并查询 xml,如下所示
try this
and query the xml like below
不完全是。
您想要查找属性具有特定值的
客户
,然后选择第二个属性。更像这样的东西:
Not quite.
You want to find a
Customer
where an attribute has a certain value, and then select a second attribute.Something more like this: