2011-04-20 96 views
1

我想顯示航空公司名稱下方的航空公司圖像。我在我的XML文件中爲airlineImage存儲了路徑。我將如何去使用LINQ在我的查詢中顯示圖像?使用LINQ查詢顯示圖像

這裏是我到目前爲止的代碼:

var query = from f in XElement.Load(MapPath("flightdata2.xml")).Elements("flight") 
        select new 
        {       
         Airline = (string)f.Element("airline"), 
         DepartureAirportSymbol = (string)f.Element("departureAirportSymbol"), 
         DepartTime = (string)f.Element("departureTime"), 
         DestinationAirportSymbol = (string)f.Element("destinationAirportSymbol"), 
         ArrivalTime = (string)f.Element("arrivalTime"), 
         Stops = (int)f.Element("numberOfStops"), 
         Duration = (string)f.Element("duration"), 
         Cabin = (string)f.Element("class"), 
         Price = "$" + (Int32)f.Element("price") 
        }; 

    this.GridView1.DataSource = query; 
    this.GridView1.DataBind(); 
+4

這聽起來好像你不應該這樣做,也許你的web應用程序首次加載xml文件時,可能會將xml處理成簡單的字典。 – IanNorton 2011-04-20 19:18:28

+1

好吧,網格中的圖片列應該託管一些圖片控件,它會給出一個圖片文件的路徑來加載實際圖片。 我不認爲,Linq與它有任何關係。它所做的只是查詢信息。 – Andrei 2011-04-20 19:59:05

回答

1

試試這個:

select new 
{ 
    Duration = (string)f.Element("duration"), 
    Cabin = (string)f.Element("class"), 
    Price = "$" + (Int32)f.Element("price") 
    ImagePath = (string)f.Element("airlineImage") 
}; 


<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false"> 
    <Columns> 
     <asp:ImageField DataImageUrlField="ImagePath" 
         DataImageUrlFormatString="~/Content/Images/{0}" /> 
    </Columns> 
</asp:GridView> 

UPDATE 1.您必須更換路徑~/Content/Images/{0}在GridView中存儲圖像的位置。 2.設置AutoGenerateColumns =「false」,這樣你的網格將只包含你定義的colums。

+0

嗨,我試過這個,但圖像似乎並沒有顯示。有一個框中顯示圖像應該出現的位置。還有我不想顯示的ImagePath列。 – multiv123 2011-04-25 18:36:53

+0

嗨,我更新了我的答案。您應該仔細檢查存儲圖像的位置。或者,檢查頁面的源代碼HTML,看看是否手動標記 jlp 2011-04-26 09:27:10

+0

Great. I got the images to display. I want to get rid of the ImagePath column from my gridview. Is there a way to hide the last column in the linq query? I tried GridView1.Columns[10].Visible = false and it says that the index was out of range. – multiv123 2011-04-26 14:43:44

1

我想你會被加載路徑到你的對象,然後動態地設置圖像的src屬性來提供最好的服務:

<img src='<%# Eval("ImageURL") %>' />