Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Linq to XML中获取正确的节点_C#_Xml_Linq_Linq To Xml - Fatal编程技术网

C# 在Linq to XML中获取正确的节点

C# 在Linq to XML中获取正确的节点,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我试图解析一个XML文件,其中包含某个频道上上传的所有视频。我正试图在一个节点中获取URL属性的值,并将其放入ViewerLocation字段中。然而,有好几个。我目前的代码是: var videos = from xElem in xml.Descendants(atomNS + "entry") select new YouTubeVideo() { Title = xElem.Element(atomNS + "title").Value, Description = xE

我试图解析一个XML文件,其中包含某个频道上上传的所有视频。我正试图在一个
节点中获取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中第一个名为
的节点作为条目,正如您所期望的那样。但是,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来表达这一点

谢谢


丹尼。

大概是这样

where xElem.Element(atomNS + "content").Attribute("type").Value == "video/3gpp"
编辑:我不太知道如何扩展和解释这一点,而不假设OP对Linq一无所知。您希望进行原始查询

from xElem in xml.Descendants(atomNS + "entry") 
where xElem.Element(atomNS + "content").Attribute("type").Value == "video/3gpp"
select new YouTubeVideo() { 
  ...
}
您可以查询节点的属性,就像查看文档的元素一样。如果有多个元素具有该属性,那么您可以(假设您总是想要找到的第一个元素)

我更改了原来的帖子,因为我相信您查询的节点是元素(atomNS+“content”),而不是顶级xElem,它使用XPath(因为我知道如何使用它)和相关的Get方法:

string videoType = "video/3gpp";

XElement root = XElement.Load(file); // or .Parse(xmlstring)
var videos = root.XPath("//entry")
    .Select(xElem => new YouTubeVideo()
    {
        Title = xElem.Get("title", "title"),
        Description = xElem.Get("content", "content"),
        DateUploaded = xElem.Get("published", "published"),
        ThumbnailLocation = xElem.XGetElement("group/content[@type={0}]/url", "url", videoType),
        ViewerLocation = xElem.XGetElement("group/content[@type={0}]/url", "url", videoType)
    });
如果视频类型没有更改,可以将XGetElement替换为:

xElem.XGetElement("group/content[@type='video/3gpp']/url", "url")

不必使用库来指定名称空间,这会更简洁。您可以查看Microsoft的
XPathSelectElements()
XPathSelectElement()
,但是它们要求您指定名称空间,并且在imo中没有很好的
Get
方法。需要注意的是,该库不是一个完整的XPath实现,但它可以与上述方法一起使用。

对此表示抱歉,一开始我不确定如何扩展。但这很有帮助,因为我认为我最初犯了一个错误。现在添加where原因意味着没有任何内容返回到var:/n您在帖子中提到它会得到第一个条目,这是否意味着您只得到第一个元素,而不是全部三个?如果是这样的话,那就是为什么你现在什么都没有得到,因为你已经放弃了那一个结果。过去,列表中的项目包括标题、说明、上载日期、缩略图位置和查看位置。然而,ViewerLocation中没有我想要的内容。现在列表为空,并且根本不包含Youtube视频对象。
xElem.XGetElement("group/content[@type='video/3gpp']/url", "url")