I'm trying to create a downloading app that runs on RSS Feeds. What I'm trying to accomplish is a get parse the xml with a title desc and a link is a listbox(which I have done) but when the user clicks on the link to then download a JPG to the phone, which I can't do any help would be greatly appreciated.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("Https://feed.xml"));
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XElement xmlitems = XElement.Parse(e.Result);
List<XElement> elements = xmlitems.Descendants("item").ToList();
List<RSSItem> aux = new List<RSSItem>();
foreach (XElement rssItem in elements)
{
RSSItem rss = new RSSItem();
rss.Description1 = rssItem.Element("description").Value;
rss.Link1 = rssItem.Element("link").Value;
rss.Title1 = rssItem.Element("title").Value;
aux.Add(rss);
TextBlock tbTitle = new TextBlock();
tbTitle.Text = rss.Title1 + "\n";
ListBoxRss.Items.Add(tbTitle);
TextBlock tbDescription = new TextBlock();
tbDescription.Text = rss.Description1 + "\n";
ListBoxRss.Items.Add(tbDescription);
TextBlock tbLink = new TextBlock();
tbLink.Text = rss.Link1 + "\n";
ListBoxRss.Items.Add(tbLink);
}
}
The Info you gave me worked fine but im having one last issue, the app loads fine lists the xml fine, when the user clicks on the download link it only works once, once downloaded if the user clicks on another one it only remembers the title and link from the first click until i restart the app here is my current code
public string fileurl;
public string filetitle;
public string filesave;
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("https://feed.xml"));
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XElement xmlitems = XElement.Parse(e.Result);
List<XElement> elements = xmlitems.Descendants("item").ToList();
List<RSSItem> aux = new List<RSSItem>();
foreach (XElement rssItem in elements)
{
RSSItem rss = new RSSItem();
rss.Description1 = rssItem.Element("description").Value;
rss.Link1 = rssItem.Element("link").Value;
rss.Title1 = rssItem.Element("title").Value;
aux.Add(rss);
TextBlock tbTitle = new TextBlock();
tbTitle.Text = rss.Title1 + "\n";
ListBoxRss.Items.Add(tbTitle);
TextBlock tbLink = new TextBlock();
tbLink.Text = "Download: " + rss.Link1;
tbLink.MouseLeftButtonDown += new MouseButtonEventHandler(tbLink_MouseLeftButtonDown);
ListBoxRss.Items.Add(tbLink);
fileurl = rss.Link1;
filetitle = rss.Description1;
}
}
private void tbLink_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri(fileurl));
}
async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
filesave = (filetitle + ".zip");
byte[] buffer = new byte[e.Result.Length];
await e.Result.ReadAsync(buffer, 0, buffer.Length);
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile.OpenFile(filesave, FileMode.Create))
{
await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
try
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile pdffile = await local.GetFileAsync(filesave);
Windows.System.Launcher.LaunchFileAsync(pdffile);
}
catch (Exception)
{
MessageBox.Show("File Not Found");
}
}