Extract image url from RSS feed

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
I am trying to extract the image url from a rss feed ...but the image url is wrapped in the <enclosure url> tag.......so my question is ..how would I extract the value from the enclosure tag?

here is the feed data.....

<rss version="2.0">
<channel>
<title>Beverly Hills Car Club RSS 2.0 Feed</title>
<link>http://www.beverlyhillscarclub.com/</link>
<description>Beverly Hills Car Club RSS 2.0 Feed</description>
<language>en-us</language>
<item>
<title>1969 Alfa Romeo Duetto</title>
<description>
<=!=[=C=D=A=T=A=[
1969 Alfa Romeo Duetto <br /> Stock # 03077, Mileage: 0, VIN # <br /> Price: $17,500<br /> Exterior Color: Silver, Interior Color: <br /> <div style="text-align: center;"><span style="color: rgb(255, 0, 0);"><span style="font-size: xx-large;"><em><strong> <span style="font-family: A
]=]=>


<=!=[=C=D=A=T=A=[
rial, Helvetica, sans-serif;">1969 Alfa Romeo Duetto with 2 Tops</span></strong></em></span></span></div><br style="font-family: Arial, Helvetica, sans-serif;" /><span style="font-size: large;"><span style="font-family: Arial, Helvetica, sans-serif;">1969 Alfa Romeo Duetto, 2 tops, silver with red interior, beautiful color combination, covered headlights, very clean and detailed engine bay, solid undercarriage, nice weekend driver that is mechanically sound. For $17,500</span><br style="font-family: Arial, Helvetica, sans-serif;" /><br style="font-family: Arial, Helvetica, sans-serif;" /><span style="font-family: Arial, Helvetica, sans-serif;">If you have any additional questions <strong><span style="color: rgb(0, 255, 0);">Please call 310-975-0272</span></strong> or email with any questions! We also welcome all international buyers. We can help with shipping quotes and arrangements.</span></span>
]=]=>



</description>


<link>
1969 Alfa Romeo Duetto
</link>


<enclosure url="http://www.beverlyhillscarclub.com/galleria_images/2078/2078_main_t.jpg" length="2791" type="image/jpeg"/> <----------------- this is what I need ------------------>
<guid>
1969 Alfa Romeo Duetto
</guid>


<pubDate>Wed, 16 Oct 2013 03:25:21 CDT</pubDate>

</item>

I tried this code

foreach (XmlNode node in nodes)
                    {
                        if (node != null)
                        {
                           
                            
                            dm.ItemTitle = node["title"].InnerText;
                            dm.Links = node["link"].InnerText;
                            dm.Description = node["description"].InnerText;
                            dm.ImageUrl = node.Attributes["enclosure url"].Value;
                           
                            string description = dm.Description;
                            string[] seperator = { " ", "src=" };
                            string Value = description;
                            string[] imagelink = Value.Split(seperator, StringSplitOptions.None);
                           
                            string imagelink_final = imagelink[2].Replace('"', ' ').Trim();
                            ImageLink.Add(imagelink[2].Replace('"', ' ').Trim());


                          
                            ListViewItem lvi = new ListViewItem(dm.ItemTitle);
                            lvi.SubItems.Add(dm.Description);
                            lvi.SubItems.Add(dm.Links);
                            lvi.SubItems.Add(" ");
                            lvi.SubItems.Add(dm.ImageUrl);
                            listView1.Items.Add(lvi);
                           


but the value of ImageUrl is always null.....

thank you for your help
-InkedGFX





 
Presumably this:
dm.ImageUrl = node.Attributes["enclosure url"].Value;
should be this:
dm.ImageUrl = node["enclosure"].Attributes["url"].Value;
 
Presumably this:
dm.ImageUrl = node.Attributes["enclosure url"].Value;
should be this:
dm.ImageUrl = node["enclosure"].Attributes["url"].Value;


yes ,,,,this worked this morning...but isnt working tonight......why would it work , then not?

InkedGFX
 
What actually happened when it didn't work? If you examine WHAT happened then you can determine WHY, but determining the cause without the symptom is rather difficult.
 
when I run the program..I get a "object not set to an instance of an object" NullRefrenceError - ...I try to check the node to see if its null before I try to extract any info.....but I get errors everytime....this is all the code for the method BackgroundWorker_DoWork()

 foreach (string link in links)            {


                // string url = lstLinks.SelectedIndex.ToString();
                string url = link;
                XmlDocument xmlDoc = new XmlDocument();
                // xmlDoc.Load(links[lstLinks.SelectedIndex]);
                xmlDoc.Load(link);
                XmlElement root = xmlDoc.DocumentElement;
                XmlNodeList nodes = root.SelectNodes("channel/item");


                // List<DownloadManager> listDM = new List<DownloadManager>();
                // int counter = 0;
                // int progress;
                foreach (XmlNode node in nodes)
                {


                    if (node["enclosure"].Attributes["url"].Value != null) <----------------- this is the eror
                    {
                        string ItemTitle = node["title"].InnerText;
                        string Links = node["link"].InnerText;
                        string Description = node["description"].InnerText;
                        string PubDate = node["pubDate"].InnerText;
                        string ImageUrl = node["enclosure"].Attributes["url"].Value;




                        try
                        {


                            // listView1.BeginUpdate();
                            ListViewItem lvi = new ListViewItem(ItemTitle);
                            lvi.SubItems.Add(Description);
                            lvi.SubItems.Add(" ");
                            lvi.SubItems.Add(Links);
                            lvi.SubItems.Add(" ");
                            lvi.SubItems.Add(ImageUrl);
                            listView1.Items.Add(lvi);
                            // listView1.EndUpdate();
                            rssData.Add(ItemTitle + "*" + ImageUrl + "*" + Description + "*" + Links + "*" + ImageUrl);


                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }


                    }
                    else
                    {




                        string ItemTitle = node["title"].InnerText;
                        string Links = node["link"].InnerText;
                        string Description = node["description"].InnerText;
                        // PubDate & ImageUrl are null - can't add the info
                        // string PubDate = node["pubDate"].InnerText;
                        // string ImageUrl = node["enclosure"].Attributes["url"].Value;


                        string description = dm.Description;
                        string[] seperator = { " ", "src=" };
                        string Value = description;
                        string[] imagelink = Value.Split(seperator, StringSplitOptions.None);
                        MessageBox.Show(imagelink[2].Replace('"', ' ').Trim());
                        string imagelink_final = imagelink[2].Replace('"', ' ').Trim();
                        ImageLink.Add(imagelink[2].Replace('"', ' ').Trim());


                        try
                        {
                            ListViewItem lvi = new ListViewItem(ItemTitle);
                            lvi.SubItems.Add(Description);
                            lvi.SubItems.Add(" ");
                            lvi.SubItems.Add(Links);
                            lvi.SubItems.Add(" ");
                            lvi.SubItems.Add(imagelink_final);
                            listView1.Items.Add(lvi);


                            rssData.Add(ItemTitle + "*" + Description + "*" + Links + "*" + imagelink_final);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                  }
                }
              }
            }


thank you for your patience and help

-InkedGFX
 
Well, you know you get a NullReferenceException and you know which line it is thrown on, so what reference on that line is null? Obviously there's either no node with that name or that node has no attribute with that name.
 
I know some of the rss feeds have a "pubDate" and/or a image link in the "enclosure" tag.......the problem is I can't check to see if the pubDate or the Enclosure tag is null.....here is the code

if (pubDate != null && node["enclosure"].Attributes["url"].Value != null)


still throws a nullrefrenceexception......

-InkedGFX
 
If there's no 'enclosure' element then 'node["enclosure"]' will be null, so you can't access its Attributes property. If there is an 'enclosure' element but it has no 'url' attribute then 'node["enclosure"].Attributes["url"]' will be null, so you can't access its Value property. You have to ensure that everything that comes before a dot is not null, otherwise using the dot to access a member will throw a NullReferenceException. That's what a NullReferenceException is: accessing an instance member of an instance that doesn't exist. You need to check each individual reference to make sure that it's not null before using it, e.g.
string enclosureUrl = null;

if (node["enclosure"] != null && node["enclosure"].Attributes["url"] != null)
{
    enclosureUrl = node["enclosure"].Attributes["url"].Value;
}
 
yes...thank you...I have been trying to figure out how to do this .......works perfectly......
would you be willing to help me get the datagridview to work correctly?


-InkedGFX
 
would you be willing to help me get the datagridview to work correctly?
Once you have a populated List, assign it to the DataSource of the DataGridView. That's it, that's all. If you have a specific issue then please describe that specifically.
 
Do you think it is better to use a data grid or a list view?
A DataGridView is a grid control and a ListView is not. The whole point of the ListView is the multiple views. If you would only be using Details view then don't use a ListView unless you specifically need the grouping functionality it provides. A DataGridView is better for purely tabular data for a number of reasons.
 
here is another issue that I just encountered.....what if within the rss feed there are some nodes that don't have the <enclosure url> tag at all ...I am getting a null refrence error ..so I scrolled thru the rss feed that was giving the error and found that the rss feed doesn't always have the enclosure tag....when the foreach loop reaches that it throws an error......how do I check to make sure there is even a enclosure tag?

thank you for your help

-InkedGFX
 
ok.....with a little thought into what I needed to do I figured it out.....here is the code I came up with.... really pretty simple...the program is working perfectly now......


string PubDate = null; 
                   string ImageUrl = null;


                    if (node["enclosure"] != null && node["enclosure"].Attributes["url"].Value != null
                        || node["pubDate"] != null && node["pubDate"].InnerText != null)
                    {
                        string ItemTitle = node["title"].InnerText;
                        string Links = node["link"].InnerText;
                        string Description = node["description"].InnerText;
                        PubDate = node["pubDate"].InnerText;


                        if (node.SelectSingleNode("enclosure") != null)
                        {
                            
                            ImageUrl = node["enclosure"].Attributes["url"].Value;
                        }
                        else
                        {
                            ImageUrl = "No Data";
                        }




-InkedGFX
 
Back
Top Bottom