0

我正在尝试用 C# 解析 iTunes 播客 XML 提要,但遇到了麻烦。它成功下载提要并将其放入 XmlDocument 对象(已测试)。之后,它进入 for-each 行,但永远不会进入循环。我不知道为什么它说频道/项目中没有任何元素(至少这是我此时的想法)。继承人的代码:

    string _returnedXMLData;
    XmlDocument _podcastXmlData = new XmlDocument();

    public List<PodcastItem> PodcastItemsList = new List<PodcastItem> ();

    _podcastXmlData.Load(@"http://thepointjax.com/Podcast/podcast.xml");

    string title = string.Empty;
    string subtitle = string.Empty;
    string author = string.Empty;

    foreach (XmlNode node in _podcastXmlData.SelectNodes(@"channel/item")) {
        title = node.SelectSingleNode (@"title").InnerText;
        subtitle = node.SelectSingleNode (@"itunes:subtitle").InnerText;
        author = node.SelectSingleNode (@"itunes:author").InnerText;
        PodcastItemsList.Add (new PodcastItem(title, subtitle, author));
    }
}

提前感谢您的任何帮助!非常感谢!

柯克兰

4

2 回答 2

2

离开我的评论,我只会使用XDocument

 var xml = XDocument.Load("http://thepointjax.com/Podcast/podcast.xml");

 XNamespace ns = "http://www.itunes.com/dtds/podcast-1.0.dtd";
 foreach (var item in xml.Descendants("item"))
 {
     var title = item.Element("title").Value;
     var subtitle = item.Element(ns + "subtitle").Value;
     var author = item.Element(ns + "author").Value;

     PodcastItemsList.Add (new PodcastItem(title, subtitle, author));
 }

itunes是 XML 中的命名空间,因此您需要使用 anXNamespace来说明它。

于 2014-11-09T03:25:53.120 回答
0

仅供参考,Apple 网站说 iTunes 命名空间链接区分大小写。我还没有使用 version="2.0" 部分,但到目前为止我还不需要它。我使用的是从其他地方复制的链接,即“...DTDs/Podcast-1.0.dtd”。只有在将其更改为小写之后,我的 RSS 阅读器中的解析才开始工作。

Apple 文档的屏幕截图

于 2017-06-11T12:09:13.657 回答