【发布时间】:2012-07-11 12:09:38
【问题描述】:
我正在尝试解析包含某个频道上所有上传视频的 XML 文件。我试图在 <media:content> 节点之一中获取 URL 属性的值并将其放入 ViewerLocation 字段中。但是有几个。我目前的代码是这样的:
var videos = from xElem in xml.Descendants(atomNS + "entry")
select new YouTubeVideo()
{
Title = xElem.Element(atomNS + "title").Value,
Description = xElem.Element(atomNS + "content").Value,
DateUploaded = xElem.Element(atomNS + "published").Value,
ThumbnailLocation = xElem.Element(mediaNS + "group").Element(mediaNS + "content").Attribute("url").Value,
ViewerLocation = xElem.Element(mediaNS + "group").Element(mediaNS + "content").Attribute("url").Value
};
它让我得到了 XML 中的第一个节点,用于输入名称为 <media:content> 的条目,正如您所期望的那样。但是,XML 中的第一个条目不是我想要的。我想要第二个。
下面是相关的 XML。
<!-- I currently get the value held in this node -->
<media:content
url='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'
type='application/x-shockwave-flash' medium='video'
isDefault='true' expression='full' duration='215' yt:format='5'/>
<!-- What i actually want is this one -->
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIAEQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='1'/>
<media:content
url='rtsp://rtsp2.youtube.com/ChoLENy73bIDRQ1kgGDA==/0/0/0/video.3gp'
type='video/3gpp' medium='video'
expression='full' duration='215' yt:format='6'/>
我想要第二个节点,因为它的类型为“video/3gpp”。我将如何选择那个?我的逻辑是
如果属性(type == "video/3gpp") 得到这个值。
但我不知道如何在 Linq 中表达。
谢谢,
丹尼。
【问题讨论】:
标签: c# xml linq linq-to-xml