Reading non-standard elements in a SyndicationItem with SyndicationFeed

前端 未结 5 640
挽巷
挽巷 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:15

    Here is how I managed to retrieve the enclosure link from a feed using SyndicationFeed.

    static void Main(string[] args)
    {
        var feedUrl = "http://blog.stackoverflow.com/index.php?feed=podcast";
    
        using (var feedReader = XmlReader.Create(feedUrl))
        {
            var feedContent = SyndicationFeed.Load(feedReader);
    
            if (null == feedContent) return;
    
            foreach (var item in feedContent.Items)
            {
                Debug.WriteLine("Item Title: " + item.Title.Text);
    
                Debug.WriteLine("Item Links");
                foreach (var link in item.Links)
                {
                    Debug.WriteLine("Link Title: " + link.Title);
                    Debug.WriteLine("URI: " + link.Uri);
                    Debug.WriteLine("RelationshipType: " + link.RelationshipType);
                    Debug.WriteLine("MediaType: " + link.MediaType);
                    Debug.WriteLine("Length: " + link.Length);
                }
            }
        }
    }
    

    The output is as follows:

    Item Title: Podcast #50
    Item Links
    Link Title:
    URI: http://blog.stackoverflow.com/2009/04/podcast-50/
    RelationshipType: alternate
    MediaType:
    Length: 0
    Link Title:
    URI: http://itc.conversationsnetwork.org/audio/download/ITC.SO-Episode50-2009.04.21.mp3
    RelationshipType: enclosure
    MediaType: audio/mpeg
    Length: 36580016

    You can identify the enclosure link from its relationship type.

提交回复
热议问题