0

特定のチャンネルにアップロードされたすべての動画を含む 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
};

<media:content>期待どおりの名前で、エントリ用の XML の最初のノードを取得します。ただし、XML の最初のエントリは、私が望むものではありません。2番目が欲しい。

以下は、関連する 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」であるため、2 番目のノードが必要です。それを選択するにはどうすればよいですか?私の論理は

if attribute(type == "video/3gpp") はこの値を取得します。

しかし、これをLinqで表現する方法がわかりません。

ありがとう、

ダニー。

4

2 に答える 2

0

関連する Get メソッドを使用して、このXml ライブラリの XPathを使用します (使用方法を知っているため)。

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()がありません。Getライブラリは完全な XPath 実装ではないことに注意してください。

于 2012-07-12T14:07:33.467 に答える
0

おそらく次のようなものです。

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() { 
  ...
}

ドキュメントの要素を見ることができるのと同じように、ノードの属性を調べることができます。その属性を持つ要素が複数ある場合は、それが可能です (常に最初に見つけた要素が必要であると仮定します)。

  ( from xElem in xml.Descendants(atomNS + "entry") 
    where xElem.Element(atomNS + "content").Attribute("type").Value == "video/3gpp"
    select new YouTubeVideo() { 
      ...
    }).First();

元の投稿を変更しました。クエリしているノードは Element(atomNS + "content") であり、最上位の xElem ではありません

于 2012-07-11T12:11:42.920 に答える