我曾尝试使用此代码在列表框中插入供稿,但在YouTube供稿中却无法使用。抱歉,但是我对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);
}


谢谢你的回答

最佳答案

命名空间在选择xml文档中的节点方面起着重要作用。您必须添加一个namespacemanger并明确指出所需的元素。选择属性(用于链接)也不同于元素的内部文本。

     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);
    }


SelectNodes with a namespacemanager

关于c# - 在列表框中输入YouTube错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8748251/

10-09 01:29