从xml解析wp7应用程序中的图像

时间:2012-08-13 12:33:16

标签: c# xml windows-phone-7

我很 遵循本教程:http://www.developer.nokia.com/Community/Wiki/Employees_app_with_XML_parsing_and_messaging_in_WP7

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

在这里,您可以看到我使用的Feed:

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

这是我的Events类:

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

任何帮助都是apreciated,tnx in advice!

迭。

1 个答案:

答案 0 :(得分: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