即使引用了 System.Xml,XmlReader 还是在不同的程序集中声明? [英] XmlReader is declared in a different Assembly even though System.Xml is referenced?

查看:26
本文介绍了即使引用了 System.Xml,XmlReader 还是在不同的程序集中声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名在大学学习计算机工程的学生,我正在尝试开发一个应用程序,该应用程序将从特定 url 读取 rss 提要,然后显示提要中每个项目的标题和链接作为通知,每当网址上的提要已更新.嗯,我实际上处于起步阶段,我正在研究这个项目以进行学习,遵循一些教程等.

I am a student studying Computer Engineering in University, and I am trying to develop an application that will read an rss feed from a certain url, then display the titles and links of each item in the feed as a notification whenever a the feed on the url is updated. Well, I am actually at the very beginning, and I am working on this project for learning purposes, following some tutorials etc.

我的计划是使用 System.ServiceModel.Syndication 库使用 SyndicationFeed 对象及其方法从 url 读取 rss 提要.但是每当我尝试使用它时,我都会遇到一个奇怪的错误.报错如下

My plan was to use System.ServiceModel.Syndication library to read the rss feed from the url using the SyndicationFeed object and its methods. But whenever I try to use that I get a strange error. The error is as follows

--- CS0012:类型XmlReader"是在未引用的程序集中定义的.您必须添加对程序集 'System.Xml, Version=5.0.5.0',Culture=neutral, PublicKeyToken='7cec85d7bea7798e' 的引用.

这是显示此错误的代码部分:

Here is the part of code that this error is shown:

    public void GetFeed()
    {
        // Create an xml reader that will read rss data from the given url
        var xmlReader = XmlReader.Create(rssUrl);
        syndicationFeed = SyndicationFeed.Load(xmlReader);
    }

我创建 xmlReader 的部分没有错误,我还引用了以下程序集System.Xml".

The part where I create the xmlReader has no errors, I also have the following assembly referenced, 'System.Xml'.

using System.Text;
using System.Threading.Tasks;
using System.ServiceModel.Syndication;
using System.Xml;     // Here is the System.Xml

此外,尝试通过右键单击并选择添加引用"来向上述库(System.Xml)添加引用只会给我另一个错误,告诉我我无法引用System.Xml",因为它已经存在由构建系统引用.

Also, trying to add a refenrence to the said library (System.Xml) by right clicking and selecting 'Add Reference' just gives me another error, telling me that I cannot refenrence 'System.Xml' as it is already being referenced by the build system.

我尝试使用 System.ServiceModel.Syndication 命名空间中的其他类来确保问题不在于程序集,并且所有其他类、方法等都可以正常工作.例如,我可以写这个并且没有错误:

I tried using other classes from the System.ServiceModel.Syndication namespace to ensure that the problem is not with the assembly, and every other class, method, etc. worked without errors. For example, I am able to write this and get no error:

SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent("Me");
item.Links.Add(new SyndicationLink() { Uri = new Uri("http://somesite.con") });
item.PublishDate = DateTime.Now;

我在上面的代码中没有发现错误.例如,当我像这样使用 XmlReader 时,我不会出错:

I get no errors on the above piece of code. I don't get errors when I use XmlReader like this for example:

  var reader = XmlReader.Create(rssUrl);
  while (reader.Read())
  {
       switch (reader.NodeType)
       {
            case XmlNodeType.Attribute:
                // Some code here
                break;

                // Some more cases here......
       }
  }

我在这里也没有收到关于 XmlReader 的错误.我只在将 XmlReader 的实例传递给 SyndicationFeed.Load(XmlReader instance) 方法时收到错误.

I get no errors here about the XmlReader either. I only get the error when passing an instance of XmlReader to a SyndicationFeed.Load(XmlReader instance) method.

// This always gives me error!!!
syndicationFeed = SyndicationFeed.Load(xmlReader);

我尝试解决这个问题已经有一段时间了,将近6个小时,我在网上搜索,引用了不同版本的System.ServiceModel.Syndication.dll,试图在Nuget包管理器上找到Syndication包.没有任何效果.作为最后的手段,我在这里问这个问题,任何帮助将不胜感激.

I have been trying to solve this problem for quite a while now, nearly 6 hours, I searched on the web, referenced different versions of System.ServiceModel.Syndication.dll, trying to find Syndication packages on Nuget package manager. Nothing worked. I am asking this question here as a last resort, and any help would be greatly appreciated.

推荐答案

UWP 应用使用 Windows 运行时类 Windows.Web.Syndication.SyndicationFeed 而不是 .Net 的 System.ServiceModel.Syndication.

UWP apps use the Windows Runtime class Windows.Web.Syndication.SyndicationFeed rather than .Net's System.ServiceModel.Syndication.

Windows.Web.Syndication.SyndicationFeed 没有 XmlReader 构造函数.通常,您将创建一个 SyndicationClient,然后调用 RetrieveFeedAsync(url) 以获取 SyndicationFeed.

Windows.Web.Syndication.SyndicationFeed doesn't have an XmlReader constructor. Generally you'll create a SyndicationClient and then call RetrieveFeedAsync(url) to get the SyndicationFeed.

请参阅如何访问网络提要 (XAML) 以获得完整的演练.

See How to access a web feed (XAML) for a full walkthrough.

这篇关于即使引用了 System.Xml,XmlReader 还是在不同的程序集中声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆