带有XmlReader的W8电话System.Xml.XmlException

本文关键字:System Xml XmlException 电话 W8 XmlReader 带有 | 更新日期: 2023-09-27 18:27:51

我正在尝试为Windows Phone 8制作一个RSS应用程序,但每当我尝试使用XmlReader下载RSS内容时,就会出现错误。

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();

在"XmlReader reader=XmlReader.Create…"行中发现错误。完整的错误消息为:

System.Xml.ni.dll 中首次出现类型为"System.Xml.XmlException"的异常

附加信息:无法打开http://feeds.bbci.co.uk/news/business/rss.xml"。Uri参数必须是文件系统相对路径。

谢谢你的帮助!:)

带有XmlReader的W8电话System.Xml.XmlException

您得到这个错误是因为它需要LocalStorage中的文件而不是网址。所以你必须下载文件并将其转换为这样:

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()
}