Reading non-standard elements in a SyndicationItem with SyndicationFeed

前端 未结 5 650
挽巷
挽巷 2020-12-02 09:08

With .net 3.5, there is a SyndicationFeed that will load in a RSS feed and allow you to run LINQ on it.

Here is an example of the RSS that I am loading:

<         


        
5条回答
  •  借酒劲吻你
    2020-12-02 09:33

    You can use a combination of LINQ and XPathNavigator to extract the syndication extensions of a feed item (based on namespace URI of the extension). For item enclosures, you will want to examine the item links collection for links that have a RelationshipType of enclosure.

    Example:

    HttpWebRequest webRequest   = WebRequest.Create("http://www.pwop.com/feed.aspx?show=dotnetrocks&filetype=master") as HttpWebRequest;
    
    using (Stream stream = webRequest.GetResponse().GetResponseStream())
    {
        XmlReaderSettings settings  = new XmlReaderSettings();
        settings.IgnoreComments     = true;
        settings.IgnoreWhitespace   = true;
    
        using(XmlReader reader = XmlReader.Create(stream, settings))
        {
            SyndicationFeed feed    = SyndicationFeed.Load(reader);
    
            foreach(SyndicationItem item in feed.Items)
            {
                // Get values of syndication extension elements for a given namespace
                string extensionNamespaceUri            = "http://www.itunes.com/dtds/podcast-1.0.dtd";
                SyndicationElementExtension extension   = item.ElementExtensions.Where(x => x.OuterNamespace == extensionNamespaceUri).FirstOrDefault();
                XPathNavigator dataNavigator            = new XPathDocument(extension.GetReader()).CreateNavigator();
    
                XmlNamespaceManager resolver    = new XmlNamespaceManager(dataNavigator.NameTable);
                resolver.AddNamespace("itunes", extensionNamespaceUri);
    
                XPathNavigator authorNavigator      = dataNavigator.SelectSingleNode("itunes:author", resolver);
                XPathNavigator subtitleNavigator    = dataNavigator.SelectSingleNode("itunes:subtitle", resolver);
                XPathNavigator summaryNavigator     = dataNavigator.SelectSingleNode("itunes:summary", resolver);
                XPathNavigator durationNavigator    = dataNavigator.SelectSingleNode("itunes:duration", resolver);
    
                string author   = authorNavigator != null ? authorNavigator.Value : String.Empty;
                string subtitle = subtitleNavigator != null ? subtitleNavigator.Value : String.Empty;
                string summary  = summaryNavigator != null ? summaryNavigator.Value : String.Empty;
                string duration = durationNavigator != null ? durationNavigator.Value : String.Empty;
    
                // Get attributes of  element
                foreach (SyndicationLink enclosure in item.Links.Where(x => x.RelationshipType == "enclosure"))
                {
                    Uri url             = enclosure.Uri;
                    long length         = enclosure.Length;
                    string mediaType    = enclosure.MediaType;
                }
            }
        }
    }
    

提交回复
热议问题