2012-08-13 33 views
1

我是 下面這個教程:http://www.developer.nokia.com/Community/Wiki/Employees_app_with_XML_parsing_and_messaging_in_WP7解析圖像中的WP7應用程序的XML

我有這樣的應用程序,顯示在我的城市的事件,每個事件都得到了一個形象,一個標題,如果我點擊一個事件我我會進入細節頁面,向我展示更大的圖像,標題和事件描述,所有與教程類似的內容,實際上它工作得很好,問題是什麼?它只顯示我一個事件。

在這裏你可以看到,我使用的飼料:

http://th05.deviantart.net/fs70/PRE/f/2012/226/0/7/xml_by_javabak-d5b1d16.png

這是我的活動類:

namespace Bluey 
{ 
    [XmlRoot("rss")] 
    public class Eventi 
    { 
     [XmlArray("channel")] 
     [XmlArrayItem("item")] 
     public ObservableCollection<Evento> Collect { get; set; } 
    } 
} 

,這是我的事件類

namespace Bluey 
{ 
    public class Evento 
    { 
    [XmlElement("title")] 
    public string title { get; set; } 
    [XmlElement("enclosure")] 
    public string image { get; set; } 
    [XmlElement("description")] 
    public string description { get; set; } 
    } 
} 

我注意到在事件類中,如果我將[XmlElement(「enclosure」)]更改爲[Xm lElement( 「URL」)]我會得到所有的事件,但沒有圖像

這是我的解析器

public EventiPage() 
    { 
     InitializeComponent(); 
     // is there network connection available 
     if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) 
     { 
      MessageBox.Show("No network connection available!"); 
      return; 
     } 
     // start loading XML-data 
     WebClient downloader = new WebClient(); 
     Uri uri = new Uri("http://www.applinesrl.com/appoggio/events/feed", UriKind.Absolute); 
     downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(EventiDownloaded); 
     downloader.DownloadStringAsync(uri); 

    } 

    void EventiDownloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Result == null || e.Error != null) 
     { 
      MessageBox.Show("There was an error downloading the XML-file!"); 
     } 
     else 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(Eventi)); 
      XDocument document = XDocument.Parse(e.Result); 
      Eventi eventi = (Eventi)serializer.Deserialize(document.CreateReader()); 
      EventiList.ItemsSource = eventi.Collect; 
     } 
    } 

任何幫助將apreciated,TNX在諮詢!

迭戈。

回答

0

根據您發佈的示例XML,您的圖像似乎嵌入到機箱節點內。所以,Evento類是部分正確的。所有你需要做的就是這樣的事情:

public class Enclosure 
{ 
    [XmlElement("url")] 
    public string url { get; set; } 
} 

public class Evento 
{ 
    [XmlElement("title")] 
    public string title { get; set; } 
    [XmlElement("enclosure")] 
    public Enclosure image { get; set; } 
    [XmlElement("description")] 
    public string description { get; set; } 
} 

更新到了如何訪問圖片網址:

XmlSerializer serializer = new XmlSerializer(typeof(Eventi)); 
XDocument document = XDocument.Parse(e.Result); 
Eventi eventi = (Eventi)serializer.Deserialize(document.CreateReader()); 
foreach (Evento obj in eventi.Collect) 
{ 
    Debug.WriteLine("Title: {0}", obj.title); 
    Debug.WriteLine("Img: {0}", obj.image.url); // Retrieving image URL 
} 

注:一些在XML值都已經在,而其他base64編碼圖像是URL。所以,你應該驗證這些。請附上我的代碼和你的XML數據獲取的數據的圖像捕獲。 enter image description here

+0

謝謝,我已經嘗試過你的建議,但它在解析xml在這一行時拋出異常XDocument document = XDocument.Parse(e.Result);我現在正在尋找這種機箱嵌套的解決方案:/ – 2012-08-13 14:02:26

+0

確定我的錯,嘗試更改Feed以尋求更快的解決方案,並且忘記返回舊的Feed以獲取例外情況...我很抱歉(大概5個小時,我開始製作非常愚蠢的錯誤),無論如何,您的建議會將整個訂閱源返回給我,除了圖像 – 2012-08-13 14:44:28

+0

我不確定您是如何實現代碼的。但是,你明白現在圖像的URL會像這個image.url一樣可用。我已經更新了上面的代碼以包含如何訪問url的部分。 – 2012-08-13 16:59:11