Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W8 Phone System.Xml.XmlException with XmlReader

I am trying to make a RSS app for Windows Phone 8 but there is an error occurring whenever I try to download the RSS content using a XmlReader.

using System.Xml.Linq;
using System.Net;
using System.ServiceModel.Syndication;

XmlReaderSettings settings = new XmlReaderSettings();
                settings.DtdProcessing = DtdProcessing.Ignore;

                XmlReader reader = XmlReader.Create(http://feeds.bbci.co.uk/news/business/rss.xml, settings);
                SyndicationFeed feed = SyndicationFeed.Load(reader);
                reader.Close();

The error is found on the line "XmlReader reader = XmlReader.Create...." The full error message is:

A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.ni.dll

Additional information: Cannot open 'http://feeds.bbci.co.uk/news/business/rss.xml'. The Uri parameter must be a file system relative path.

Thank you for your help! :)

like image 717
user3795349 Avatar asked Jan 25 '26 22:01

user3795349


1 Answers

You're getting that error because it's expecting a file in LocalStorage not a web address. So you have to download the file and convert it over like so:

public MainPage()
{
    // our web downloader
    WebClient downloader = new WebClient();

    // our web address to download, notice the UriKind.Absolute
    Uri uri = new Uri("http://feeds.bbci.co.uk/news/business/rss.xml", UriKind.Absolute);

    // we need to wait for the file to download completely, so lets hook the DownloadComplete Event
    downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(FileDownloadComplete);

    // start the download
    downloader.DownloadStringAsync(uri); 
}

// this event will fire if the download was successful
void FileDownloadComplete(object sender, DownloadStringCompletedEventArgs e)
{
    // e.Result will contain the files byte for byte

    // your settings
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.DtdProcessing = DtdProcessing.Ignore;

    // create a memory stream for us to use from the bytes of the downloaded file
    MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(e.Result ?? ""));

    // create your reader from the stream of bytes
    XmlReader reader = XmlReader.Create(ms, settings);

    // do whatever you want with the reader
    // ........

    // close
    reader.Close()
}
like image 175
Chubosaurus Software Avatar answered Jan 27 '26 11:01

Chubosaurus Software