level42 level42 - 4 months ago 15
Vb.net Question

How can I extract an image from an RSS Feed?

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.

Answer
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