获得具有特定属性的所有元素在LINQ to XML
问题描述:
我一直在与一些的LINQ to XML玩耍,和我有解析http://www.nationalbanken.dk/dndk/valuta.nsf/valuta-hist.xml,这是表示在过去五天汇率列表的一些问题,丹麦。获得具有特定属性的所有元素在LINQ to XML
我试图让所有的“立方体”这里的属性的“货币”是等于一个特定的值。
到现在为止,我有以下几点:
Stream stream = e.Result;
XDocument doc = XDocument.Load(stream);
var _rd = (from x in doc.Descendants("Cube")
where x.Attribute("currency").Value.Equals(SelectedCurrency.Instance.CurrencyCode)
select x).ToList();
Ofcause这种问题已经回答过很多次了,但我不能,如果它有什么做的复制图元素名称,导致我得到的是一个空白的null。
答
你的问题是,你不使用命名空间解析XML。
XNamespace ns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
var _rd = (from x in doc.Descendants(ns+"Cube")
where x.Attribute("currency").Value.Equals(SelectedCurrency.Instance.CurrencyCode)
select x).ToList();
沿着这些线应该工作。
[使用XDocument查找元素的属性](http://stackoverflow.com/questions/2678251/find-elements-by-attribute-using-xdocument) – 2012-01-09 16:31:29