【发布时间】:2013-05-22 05:08:43
【问题描述】:
我无法从 xml 中获取属性值。我正在使用 Google 自定义搜索 API。
var results =
(from r in res.Descendants("R")
where r.Element("T") != null
select new GoogleSearchResultItem(
new PageSummary
{
Title = r.Element("T").Value,
LinkURL = r.Element("U").Value,
Description = r.Element("S").Value
}));
它适用于 T、U、S 元素,但是当我尝试获取属性 programid 时,我得到了 null。
这里是 XML:
<R N="1">
<U>
Link to somepage
</U>
<T>
<b>Title</b>
</T>
<S>
Summary
</S>
<LANG>sv</LANG>
<Label>_cse_1h5bojdlb5c</Label>
<PageMap>
<DataObject type="metatags">
<Attribute name="displaydate" value="20121028"/>
<Attribute name="programid" value="2519"/>
<Attribute name="formatid" value="116"/>
</DataObject>
</PageMap>
【问题讨论】:
-
你能在 XML 中显示 T、U 或 S 元素吗?
-
我已经更新了 XML
-
是的,T、U 和 S 工作正常,我只是使用 r.Element("T") 来获取标题。也许是因为它在 R 的根目录中。但问题是在 PageMap>DataObject 中获取属性 programid
标签: c# linq xml-parsing google-search