使用 linqToXml 从 xml 文档获取值
我就是无法弄清楚这一点。我尝试环顾四周,但我的查询不会呈现任何结果。
这是 XML:
<?xml version="1.0" encoding="utf-8" ?>
<Prices>
<PricePerItem>
<Item Name="Milk, Low fat, 1Liter">11.2</Item>
<Item Name="Butter">17</Item>
<Item Name="Bread">12.2</Item>
<Item Name="Cheese">15.5</Item>
</PricePerItem>
<PricePerKg>
<Item Name="Apple, Jonagold">13.4</Item>
<Item Name="Chicken">12.5</Item>
<Item Name="Salad">9.6</Item>
<Item Name="Fish, Salmon">14</Item>
</PricePerKg>
</Prices>
和我的方法(ofc 未完成)
private static Dictionary<string, int> GetPrizesFromXML()
{
//Read the values from the XMLDoc
XElement xml = XElement.Load("prices.xml");
var prizes = from q in xml.Elements("Prices")
select q.Elements("Item");
foreach (var prize in prizes)
{
Console.Out.WriteLine(prize.ToString());
}
return null;
}
I just can't get this figured out right. I've tried looking around but my queries won't render any results.
This is the XML:
<?xml version="1.0" encoding="utf-8" ?>
<Prices>
<PricePerItem>
<Item Name="Milk, Low fat, 1Liter">11.2</Item>
<Item Name="Butter">17</Item>
<Item Name="Bread">12.2</Item>
<Item Name="Cheese">15.5</Item>
</PricePerItem>
<PricePerKg>
<Item Name="Apple, Jonagold">13.4</Item>
<Item Name="Chicken">12.5</Item>
<Item Name="Salad">9.6</Item>
<Item Name="Fish, Salmon">14</Item>
</PricePerKg>
</Prices>
And my method (not done ofc)
private static Dictionary<string, int> GetPrizesFromXML()
{
//Read the values from the XMLDoc
XElement xml = XElement.Load("prices.xml");
var prizes = from q in xml.Elements("Prices")
select q.Elements("Item");
foreach (var prize in prizes)
{
Console.Out.WriteLine(prize.ToString());
}
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此行:更改
为:
Change this line:
to this: