问题
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.
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: The type 'XmlReader' is defined in an assembly that is not referenced. You must add a reference to assembly '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);
}
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
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.
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;
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......
}
}
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);
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.
回答1:
UWP apps use the Windows Runtime class Windows.Web.Syndication.SyndicationFeed rather than .Net's System.ServiceModel.Syndication.
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.
See How to access a web feed (XAML) for a full walkthrough.
来源:https://stackoverflow.com/questions/34957617/xmlreader-is-declared-in-a-different-assembly-even-though-system-xml-is-referenc