1

我正在关注本教程: http: //www.developer.nokia.com/Community/Wiki/Employees_app_with_XML_parsing_and_messaging_in_WP7

我有一个显示我所在城市的事件的应用程序,每个事件都有一个图像和一个标题,如果我单击一个事件,我将转到显示更大图像的详细信息页面,事件的标题和描述,一切类似于教程,实际上它工作得很好,问题吗?它只显示了一个事件。

在这里你可以看到我使用的提要:

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")] 更改为 [XmlElement("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; 
        }
    }

任何帮助将不胜感激,tnx 建议!

迭戈。

4

1 回答 1

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; }
}

更新以显示如何访问图像 URL:

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 数据获得的数据的图像捕获。在此处输入图像描述

于 2012-08-13T13:10:46.963 回答