2016-08-08 177 views
0

== [EDIT1] ======如何從RSS源提取圖像?

後續問題:

使用答案提供以下,通過FloatingKiwi,我希望能夠通過解析,找到只是第一個「圈地」,在每一個「項目」

目前的標籤,我能想出這樣的:

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

這似乎給我的第一個「圈地」標記爲先「的項目「15次(xml中的項數)

任何幫助總是讚賞:)

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

我一直在嘗試解決這個問題一段時間,但我找不到從RSS提要中提取圖片的網址。

我想解析整個RSS提要以提取圖片URL,它位於機箱標籤中,但似乎無法獲取它。

以下是我到目前爲止的代碼。

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 

下面是XML內容的一個示例:

<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> 

任何幫助,將不勝感激。

回答

1

這裏的問題是你在循環開始之前設置url,所以它始終指向第一個項目。移動網址的分配內循環,並將其指向的項目,而不是xml_items

Dim xml_items = From ImageURL In XElement.<channel>.<item> 
    For Each item As XElement In xml_items 
     Dim url = item.<enclosure>[email protected] 
     TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + url 
    Next item 
+0

啊,我新我很接近!再次感謝你FloatingKiwi。你一直是一個巨大的幫助:) – level42

1
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>[email protected] 
     TextBox1.Text = TextBox1.Text + Environment.NewLine + Environment.NewLine + url 
    Next 
End Sub 
+0

這似乎完美地工作!謝謝:) – level42

+0

對不起,後續問題。但是,你知道我怎樣才能讓它循環遍歷每個並只拉第一個標籤?不是所有的人? – level42