【问题标题】:W8 Phone System.Xml.XmlException with XmlReaderW8 电话 System.Xml.XmlException 与 XmlReader
【发布时间】:2014-10-26 22:25:52
【问题描述】:

我正在尝试为 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 参数必须是文件系统相对路径。

感谢您的帮助! :)

【问题讨论】:

    标签: c# xml windows windows-phone-8 rss


    【解决方案1】:

    您收到该错误是因为它需要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()
    }
    

    【讨论】:

    • 您好,感谢您的回答,尽管它会发出另一个错误:在 mscorlib.ni.dll 中发生“System.ArgumentException”类型的第一次机会异常附加信息:使用未定义的关键字值 1事件任务计划。 @ChubosaurusSoftware
    • @user3795349 代码中没有错误,创建新项目并将其粘贴进去。您遇到的错误是您正在做的其他事情的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 2010-11-12
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多