我試圖從XML解析,但由於某種原因,我沒有在我的文本框中被disaplyed,我有變量綁定。LINQ to XML - 沒有任何約束力?
我喜歡嘗試各種各樣的變化sof Xdocuemnt或Xelement,但它似乎並沒有工作。 XML結構看起來相當簡單,所以我無法弄清楚發生了什麼問題。
任何幫助,將不勝感激。
編輯* * ** * ** * ****
現在,所有的工作。感謝您的幫助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
namespace TradeMe
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient Trademe = new WebClient();
Trademe.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Trademe_DownloadStringCompleted);
Trademe.DownloadStringAsync(new Uri ("http://api.trademe.co.nz/v1/Search/General.xml?search_string=" + TradeSearch.Text));
}
void Trademe_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var r = XDocument.Parse(e.Result);
// Declare the namespace
XNamespace ns = "http://api.trademe.co.nz/v1";
listBox1.ItemsSource = from TM in r.Root.Descendants(ns + "Listing")
select new TradeItem
{
//ImageSource = TM.Element(ns + "Listing").Element(ns + "PictureHref").Value,
Message = TM.Element(ns + "Title").Value,
UserName = TM.Element(ns + "Region").Value
};
}
public class TradeItem
{
public string UserName { get; set; }
public string Message { get; set; }
public string ImageSource { get; set; }
}
}
}
XML看起來像這樣。
<SearchResults xmlns="http://api.trademe.co.nz/v1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<TotalCount>12723</TotalCount>
<Page>1</Page>
<PageSize>50</PageSize>
- <List>
- <Listing>
<ListingId>399739762</ListingId>
<Title>Playstation 3 320GB Slim going at $1 Reserve</Title>
<Category>0202-6205-6207-</Category>
<StartPrice>1.0000</StartPrice>
<StartDate>2011-08-14T22:52:28.833Z</StartDate>
<EndDate>2011-08-21T08:45:00Z</EndDate>
<ListingLength i:nil="true" />
<HasGallery>true</HasGallery>
<MaxBidAmount>400.0000</MaxBidAmount>
<AsAt>2011-08-18T19:33:41.4561556Z</AsAt>
<CategoryPath>/Gaming/PlayStation-3/Consoles</CategoryPath>
<PictureHref>http://images.trademe.co.nz/photoserver/thumb/27/183787627.jpg</PictureHref>
<RegionId>2</RegionId>
<Region>Auckland</Region>
<BidCount>137</BidCount>
<IsReserveMet>true</IsReserveMet>
<HasReserve>true</HasReserve>
<NoteDate>1970-01-01T00:00:00Z</NoteDate>
<ReserveState>Met</ReserveState>
<PriceDisplay>$400.00</PriceDisplay>
</Listing>
我做了一些改變,它工作正常。只有在圖像中不起作用的東西,我得到未處理的異常錯誤。有任何想法嗎?我已更新代碼 – Rhys
是的,請嘗試: ImageSource = TM.Element(ns +「PictureHref」)。Value, 您有一個額外的冗餘元素(ns +「Listing」) –