用冒号读取XML(:)

时间:2014-04-06 12:59:00

标签: c# attributes xmldocument colon selectsinglenode

我试图从以下XML文档(https://gdata.youtube.com/feeds/api/videos?q=example)获取视频的视图,我能够获取链接和autor,因为标记中没有冒号

我试图得到yt:统计数据,但我不知道如何。

    result = e.Result.Replace("xmlns='http://www.w3.org/2005/Atom' ", String.Empty);

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(result);

    XmlNodeList videos = doc.GetElementsByTagName("entry");

    foreach (XmlNode video in videos)
    {
        XmlNode insideauthor = video.SelectSingleNode("author");

        string videoId = video.SelectSingleNode("id").InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
        string author = insideauthor.SelectSingleNode("name").InnerText;

        // Trying to get the views of a video of the search results
        MessageBox.Show(video.SelectSingleNode("yt:statistics").Attributes["viewCount"].InnerText);
    }

Joery。

1 个答案:

答案 0 :(得分:3)

XmlNodeList videos = doc.GetElementsByTagName("entry");

foreach (XmlNode video in videos)
{
    string videoId = video["id"].InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
    string author = video["author"]["name"].InnerText;
    string views = video["yt:statistics"].Attributes["viewCount"].Value;

    Console.WriteLine(videoId);
    Console.WriteLine(author);
    Console.WriteLine(views);
}