【发布时间】:2012-09-30 08:59:24
【问题描述】:
所以我有这个代码:
List<PriceDetail> prices =
(from item in xmlDoc.Descendants(shop.DescendantXName)
select new PriceDetail
{
Price = GetPrice(item.Element(shop.PriceXPath).Value),
GameVersion = GetGameVersion(((IEnumerable)item.XPathEvaluate(shop.TitleXPath)).Cast<XAttribute>().First<XAttribute>().Value, item.Element(shop.PlatformXPath).Value),
Shop = shop,
Link = item.Element(shop.LinkXPath).Value,
InStock = InStock(item.Element(shop.InStockXPath).Value)
}).ToList<PriceDetail>();
我遇到的问题是这段代码:
((IEnumerable)item.XPathEvaluate(shop.TitleXPath)).Cast<XAttribute>().First<XAttribute>().Value
有时来自 XPathEvaluate 的对象可能是 XElement,然后强制转换不起作用。所以我需要的是一个适用于 XAttribute 和 XElement 的 Cast。
有什么建议吗?
【问题讨论】:
-
有时项目可能是 XElement,然后是 .. - 此代码中的
item只能 是 XElement。使问题难以理解。 -
很抱歉让您感到困惑。 XPathEvaluate 中的对象可能来自 XElement 或来自 XAttribute,具体取决于 xpath。