我曾尝试将此代码用于列表框中的插入Feed,但使用youtube Feed则无效。抱歉,但我对xml不太了解
XmlDocument RSSXml = new XmlDocument();
RSSXml.Load("http://gdata.youtube.com/feeds/api/users/google/uploads");
XmlNodeList RSSNodeList = RSSXml.SelectNodes("feed");
XmlNode RSSDesc = RSSXml.SelectSingleNode("feed");
foreach (XmlNode RSSNode in RSSNodeList)
{
XmlNode RSSSubNode;
RSSSubNode = RSSNode.SelectSingleNode("title");
string title = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("link");
string link = RSSSubNode != null ? RSSSubNode.InnerText : "";
listBox1.Items.Add(title);
}
相反,这是有效的
//Fetch the subscribed RSS Feed
XmlDocument RSSXml = new XmlDocument();
RSSXml.Load("mywebsite/feed/");
XmlNodeList RSSNodeList = RSSXml.SelectNodes("rss/channel/item");
XmlNode RSSDesc = RSSXml.SelectSingleNode("rss/channel/title");
foreach (XmlNode RSSNode in RSSNodeList)
{
XmlNode RSSSubNode;
RSSSubNode = RSSNode.SelectSingleNode("title");
string title = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("link");
string link = RSSSubNode != null ? RSSSubNode.InnerText : "";
listBox1.Items.Add(title);
listBox2.Items.Add(link);
}
感谢您的回答
答案 0 :(得分:1)
命名空间在选择xml文档中的节点时确实很有用。您必须添加名称空间管理器并明确指出您想要的元素。选择属性(对于链接)也不仅仅是元素的非文本。
XmlDocument RSSXml = new XmlDocument();
RSSXml.Load("http://gdata.youtube.com/feeds/api/users/google/uploads");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(RSSXml.NameTable);
nsmgr.AddNamespace("tns", "http://www.w3.org/2005/Atom");
XmlNodeList RSSNodeList = RSSXml.SelectNodes("//tns:entry", nsmgr);
XmlNode RSSDesc = RSSXml.SelectSingleNode("tns:feed",nsmgr);
foreach (XmlNode RSSNode in RSSNodeList) {
XmlNode RSSSubNode;
RSSSubNode = RSSNode.SelectSingleNode("tns:title", nsmgr);
string title = RSSSubNode != null ? RSSSubNode.InnerText : "";
RSSSubNode = RSSNode.SelectSingleNode("tns:link/@href",nsmgr);
string link = RSSSubNode != null ? RSSSubNode.InnerText : "";
Console.WriteLine("{0} {1}",title, link);
}