2009-04-12 98 views
0

我似乎遇到問題,並不確定如何使其工作。 我想通過使用linq和代碼工作,從XML採取數據,但是當我嘗試使用此linq數據作爲ListView的數據源時,我收到錯誤。LINQ to XML和ViewList

我該如何讓它工作?我如何將我的var變量轉換爲適當的變量,使ListView和Pager接受它?

代碼的任何示例將不勝感激。

這是我收到的錯誤:

Exception Details: System.InvalidOperationException: ListView with id 'showTopics' must have a data source that either implements ICollection or can perform data source paging if AllowPaging is true. 

Source Error: 

Line 28:   showTopics.DataSource = archievePosts; 
Line 29:   showTopics.DataBind(); 

和代碼隱藏:

String xmlpath = Server.MapPath("App_Data/topics.xml"); 
var archievePosts = from a in XElement.Load(xmlpath).Elements("topic") 
        where (DateTime.Parse(a.Element("topicdate").Value) <= DateTime.Parse(hDate.Text)) 
        select new 
        { 
         topic_id = a.Attribute("id").Value.ToString(), 
         topic_subject = a.Element("topicname").Value, 
         topic_date = a.Element("topicdate").Value 
        }; 
showTopics.DataSource = archievePosts; 
showTopics.DataBind(); 

回答

2

您可以使用ToArrayList()ToList()

但是你確定你所得到數據或有問題,請嘗試調試並查看ArchivePosts的值。

希望這會有所幫助。

+0

是的,我確定我正在獲取數據。我在調試過程中關閉了Pager,並返回了適當的值。現在嘗試你的方法。 – Dmitris 2009-04-12 21:16:48