2012-03-30 120 views
0

我想解析並顯示每個'item'在XML文件中的圖片和描述。我無法找出圖像的一部分,我只能夠以連接'description'顯示它:LINQ和XML - 解析並顯示圖像

.ASPX:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:Image ID="imgAmberIMG" runat="server" /> 

     <asp:Label ID="lblDescription" runat="server"></asp:Label> 

    </div> 
    </form> 
</body> 
</html> 

後臺代碼:

Imports System.Xml.Linq 
Imports System.Net 


Partial Class AmberAlert 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 

     Dim amberAlert As XDocument = XDocument.Load("http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss&LanguageCountry=en_US&orgPrefix=NCMC&state=NY") 

     For Each xe As XElement In amberAlert.Descendants("item") 

      lblDescription.Text += (xe.Element("description").Value) + "<br />" 
      imgAmberIMG.ImageUrl = xe.Element("enclosure").Attribute("url").Value 

     Next 

    End Sub 
End Class 

我在樹中正確的元素;我可以顯示第一張圖片,並且能夠訪問所有「描述」元素。但我需要讓每個'item'元素一起顯示。

謝謝!

+0

看看http://stackoverflow.com/questions/21877/dynamically-rendering-aspimage-from-blob-entry-in-asp-net – volody 2012-03-30 18:30:19

+0

你知道,你剛剛回答了我有關Oracle BLOB的另一個問題上個月!但是,我需要考慮如何解析XML文件... – tahdhaze09 2012-03-30 18:36:52

回答

0

使用一個asp:DataList把你的圖像和描述放在裏面,並使用數據綁定來設置它們的屬性。請參閱包含示例的MSDN上的DataList Class

代替使用爲每個:

Dim missingList = From xe As XElement In amberAlert.Descendants("item") 
        Select New With { 
             Description = (xe.Element("description").Value) + "<br />", 
             ImageUrl = xe.Element("enclosure").Attribute("url").Value 
            } 

然後設置在DataList到missingList的DataSource。