level42
level42

Reputation: 987

How can I extract an image from an RSS Feed?

====== [ Edit1 ] ======

Follow up questions:

Using the answer provide below, by FloatingKiwi, I'd like to be able to parse through and find just the first "enclosure" tag in each "item"

Currently, I was able to come up with this:

Dim xml_items = From ImageURL In xelement.<channel>.<item>
Dim url = xml_items.<enclosure>.@url
For Each item As XElement In xml_items
    TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + url
Next item

This seems to give me the first "enclosure" tag for the first "item" 15 times (the number of items in the xml)

Any assistance is always appreciated :)

===========================================================

I've been trying to sort this out for a while now, but I can't find out how to extract the URL of an Image from an RSS feed.

I'd like to parse through an entire RSS feed to extract the image url, which is in an enclosure tag, but I can't seem to get it.

Below is the code I have so far.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xelement As XElement = XElement.Load("..\\..\\example.xml")
    Dim links As IEnumerable(Of XElement) = xelement.Elements()

    Dim url = From ImageURL In xelement.Elements("item") Where CStr(ImageURL.Element("enclosure").Attribute("url")) Select ImageURL

    For Each link As XElement In url
        TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + link.Element("item").Value
    Next link
End Sub

Here is a sample of the xML content:

<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>&nbsp;<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>

Any help would be greatly appreciated.

Upvotes: 0

Views: 396

Answers (2)

FloatingKiwi
FloatingKiwi

Reputation: 4506

The problem here is that you're setting the url before the loop begins so it's pointing to the first item always. Move assignment of url inside the loop and point it to item instead of xml_items

    Dim xml_items = From ImageURL In XElement.<channel>.<item>
    For Each item As XElement In xml_items
        Dim url = item.<enclosure>.@url
        TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + url
    Next item

Upvotes: 1

FloatingKiwi
FloatingKiwi

Reputation: 4506

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim xelement As XElement = XElement.Load("..\\..\\example.xml")
    For Each item In xelement.<channel>.<item>
        Dim url = item.<enclosure>.@url
        TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + url
    Next
 End Sub

Upvotes: 1

Related Questions