我有这个档案:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://purl.org/atom/app#' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gml='http://www.opengis.net/gml' xmlns:yt='http://gdata.youtube.com/schemas/2007' xmlns:georss='http://www.georss.org/georss'>
<id>http://gdata.youtube.com/feeds/api/users/snifyy/favorites</id>
<updated>2011-09-26T16:21:40.933Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/>
<title type='text'>Favorites of snifyy</title>
<logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo>
<author>
<name>snifyy</name>
<uri>http://gdata.youtube.com/feeds/api/users/snifyy</uri>
</author>
<generator version='2.1' uri='http://gdata.youtube.com'>YouTube data API</generator>
<openSearch:totalResults>631</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>http://gdata.youtube.com/feeds/api/videos/LXO-jKksQkM</id>
<published>2011-09-23T16:15:27.000Z</published>
<updated>2011-09-26T16:21:15.000Z</updated>
<title type='text'>PUMPED UP KICKS|DUBSTEP</title>
<content type='text'>DUBSTEPPIN!!! to a beast track remixed by "butch clancy"</content>
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=LXO-jKksQkM&feature=youtube_gdata'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/LXO-jKksQkM/responses'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/LXO-jKksQkM/related'/>
<link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=LXO-jKksQkM'/>
<link rel='related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/LXO-jKksQkM'/>
<link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/users/snifyy/favorites/LXO-jKksQkM'/>
<author>
<name>WHZGUD2</name>
<uri>http://gdata.youtube.com/feeds/api/users/whzgud2</uri>
</author>
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/LXO-jKksQkM/comments' countHint='3738'/>
</gd:comments>
<media:group>
<media:category label='Entertainment' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Entertainment</media:category>
<media:content url='http://www.youtube.com/v/LXO-jKksQkM?f=user_favorites&app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='327' yt:format='5'/>
<media:content url='rtsp://v3.cache5.c.youtube.com/CioLENy73wIaIQlDQiypjL5zLRMYDSANFEgGUg51c2VyX2Zhdm9yaXRlcww=/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='327' yt:format='1'/>
<media:content url='rtsp://v1.cache2.c.youtube.com/CioLENy73wIaIQlDQiypjL5zLRMYESARFEgGUg51c2VyX2Zhdm9yaXRlcww=/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='327' yt:format='6'/>
<media:description type='plain'>DUBSTEPPIN!!! to a beast track remixed by "butch clancy"</media:description>
<media:keywords>DUBSTEP, butch, clancy, bass</media:keywords>
<media:player url='http://www.youtube.com/watch?v=LXO-jKksQkM&feature=youtube_gdata_player'/>
<media:thumbnail url='http://i.ytimg.com/vi/LXO-jKksQkM/0.jpg' height='360' width='480' time='00:02:43.500'/>
<media:thumbnail url='http://i.ytimg.com/vi/LXO-jKksQkM/1.jpg' height='90' width='120' time='00:01:21.750'/>
<media:thumbnail url='http://i.ytimg.com/vi/LXO-jKksQkM/2.jpg' height='90' width='120' time='00:02:43.500'/>
<media:thumbnail url='http://i.ytimg.com/vi/LXO-jKksQkM/3.jpg' height='90' width='120' time='00:04:05.250'/>
<media:title type='plain'>PUMPED UP KICKS|DUBSTEP</media:title>
<yt:duration seconds='327'/>
</media:group>
<gd:rating average='4.96415' max='5' min='1' numRaters='24435' rel='http://schemas.google.com/g/2005#overall'/>
<yt:statistics favoriteCount='16660' viewCount='924793'/>
</entry>
</feed>
我想加载所有条目,然后加载标题,内容和缩略图。但即使加载条目我也有问题。这是我的代码(数据是这个xml):
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
XmlNodeList xnList = xml.SelectNodes("feed/entry");
foreach (XmlNode xn in xnList) {
Debug.WriteLine(xn.LocalName.ToString());
}
答案 0 :(得分:3)
问题是您的XPath没有指定命名空间(因此您实际上是在搜索具有空命名空间URI的名称)。鉴于feed
是顶级元素,这是一个非常简单的查询 - 我不认为使用XPath实际上是在帮助你。请尝试使用XmlElement.GetElementsByTagName
:
XmlElement root = doc.DocumentElement;
String atomUri = "http://www.w3.org/2005/Atom";
foreach (XmlElement element in root.GetElementsByTagName("entry", atomUri))
{
// Use element here
}
(如果您有机会使用.NET 3.5,我建议您开始使用LINQ to XML - 它更好:)
编辑:这是一个简短但完整的程序,打印出每个url
元素的content
属性:
using System;
using System.Xml;
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlElement root = doc.DocumentElement;
String atomUri = "http://www.w3.org/2005/Atom";
String mediaUri = "http://search.yahoo.com/mrss/";
foreach (XmlElement entry in root.GetElementsByTagName("entry", atomUri))
{
foreach (XmlElement group in
entry.GetElementsByTagName("group", mediaUri))
{
foreach (XmlElement content in
entry.GetElementsByTagName("content", mediaUri))
{
Console.WriteLine(content.Attributes["url"].Value);
}
}
}
}
}
答案 1 :(得分:2)
您可以使用XmlNamespaceManager
从Feed中指定namspace:
XmlDocument xDoc = new XmlDocument();
xDoc.Load(data);
XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
XmlNodeList xnList = xDoc.SelectNodes("atom:feed/atom:entry", manager);
foreach (XmlNode xn in xnList)
{
Debug.WriteLine(xn.LocalName.ToString());
}
当您尝试选择节点时,任何类型都使用命名空间的前缀,例如:atom:feed/atom:entry
添加另一个名称空间以处理多个名称空间:
XmlNamespaceManager manager = new XmlNamespaceManager(xDoc.NameTable);
manager.AddNamespace("atom", "http://www.w3.org/2005/Atom");
manager.AddNamespace("media", "http://search.yahoo.com/mrss/");
XmlNodeList mediaNodes = xDoc.SelectNodes("atom:feed/atom:entry/media:group", manager);
答案 2 :(得分:0)
选择feed / entry元素时,需要使用Atom命名空间。
创建命名空间管理器,为Atom命名空间添加命名空间前缀,然后在XPath表达式中使用它。
例如代码,请参阅this question的接受答案。
答案 3 :(得分:0)