我試圖在應用程序開始時從我的xml文件「favorite.xml」中查詢產品名稱和產品圖像,並將其全部放在最喜歡的產品列表中。我的查詢語句是這樣從Windows Phone 8中的XML文件查詢中獲取空值
private void Favorite_Load()
{
var storage = IsolatedStorageFile.GetUserStoreForApplication();
fileName = "Favorite\\favorite.xml";
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
lst_product1.Items.Clear();
XDocument doc = XDocument.Load(isoStream);
//Check if there is favorite element in the favorite.xml file
if (doc.Root.Descendants("favorite").Count() > 0)
{
var data = from query in doc.Descendants("favorite")
orderby query.Attribute("id").Value
select new ProductsDry
{
favID = query.Attribute("id").Value,
favProCate = query.Attribute("cate_xml").Value,
favProId = query.Attribute("pro_id").Value,
favProImage = query.Attribute("pro_image").Value,
favProName = query.Attribute("pro_name").Value
};
lst_product1.ItemsSource = data; // data=null
}
isoStream.Position = 0;
isoStream.Dispose();
}
}
這裏是我的「favorite.xml」文件
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Favorite's xml-->
<favorites>
<favorite id="1" pro_id="1" pro_name="Boots Expert Anti-Blemish Cleansing Foam" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/BO001.JPG" />
<favorite id="2" pro_id="3" pro_name="Cerave foaming facial cleanser" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/CV001.JPG" />
<favorite id="3" pro_id="9" pro_name="L'Oreal Paris Go 360 Clean Anti-Breakout Facial Cleanser" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/LO003.JPG" />
<favorite id="4" pro_id="10" pro_name="Olay Foaming Face Wash - Sensitive" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/OL014.JPG" />
<favorite id="5" pro_id="22" pro_name="Alpha Hydrox Sheer Silk Moisturizer SPF 15 Sunscreen " cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/bp256.1.jpg" />
</favorites>
但是,畢竟它不會顯示在列表中任何東西,相反,它表明類似的路徑該項目,我的查詢爲空,因爲我調試它。
之前,我一直用這個代碼來讀取數據,並從XML文件,但XML文件不位於Isoloated寄存表現出來。我知道這次我從Isoloated Storage中讀到它;因此,一定有不同的東西。請幫忙,謝謝。
如果數據爲空,那麼可能您的查詢是錯誤的。您可以將XML添加到問題中。 – venerik 2014-10-01 16:58:36
@venerik對不起,這是不正確的,因爲延遲的LINQ執行,它只是null,他的查詢很好。它僅在列表視圖在運行時要求數據時執行。 – Vlad 2014-10-01 17:28:36