SyndicationFeed - 无法读取网址,错误行和位置

时间:2016-09-20 08:20:31

标签: c# asp.net

我有一些SyndicationFeed的问题,这是一个我无法弄清楚的异常,一直在寻找答案。尝试过其他用户建议的大多数解决方案,但没有运气。这是代码。

// the feed that will throw Exception. 
var stream = "http://www.gamespot.com/feeds/news/";

  try
                {
                    var settings = new XmlReaderSettings()
                    {
                        ProhibitDtd = false,
                        IgnoreComments = true
                    };

                    using (XmlReader xmlReader = XmlReader.Create(stream, settings))
                    {
                        xmlReader.Read();

                        var t = 0;

                        var rss = new Rss20FeedFormatter();
                        var atom = new Atom10FeedFormatter();
                        _feed = new SyndicationFeed();
                        // the problem is here when i try to load to  
                        // the feed with xmlReader, for some reason the 
                        //feed can't read the xmlReader. getting row and 
                        //position error.
                        if (atom.CanRead(xmlReader))
                        {
                            _feed = SyndicationFeed.Load(xmlReader);
                        }
                        else if (rss.CanRead(xmlReader))
                        {
                            _feed = SyndicationFeed.Load(xmlReader);
                        }
                        xmlReader.Close();
                    }
                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                }

similar problem 我真的需要某种打击。

提前多多感谢。

2 个答案:

答案 0 :(得分:0)

问题出在缓存节点中,在解析之前将其删除。

var stream = "http://www.gamespot.com/feeds/news/";

            try
            {
                var settings = new XmlReaderSettings()
                {
                    ProhibitDtd = false,
                    IgnoreComments = true,
                    IgnoreWhitespace=true
                };

                string xml = new WebClient().DownloadString(stream);

                XDocument doc = XDocument.Parse(xml);
                doc.Descendants().Where(e => e.Name == "cache").Remove();

                using (Stream memoryStream = new MemoryStream())  // Create a stream
                {
                    doc.Save(memoryStream);      // Save XDocument into the stream
                    memoryStream.Position = 0;   // Rewind the stream ready to read from it elsewhere

                    using (XmlReader xmlReader = XmlReader.Create(memoryStream, settings))
                    {
                        xmlReader.Read();

                        var rss = new Rss20FeedFormatter();
                        var atom = new Atom10FeedFormatter();
                        var _feed = new SyndicationFeed();
                        // the problem is here when i try to load to  
                        // the feed with xmlReader, for some reason the 
                        //feed can't read the xmlReader. getting row and 
                        //position error.
                        if (atom.CanRead(xmlReader))
                        {
                            _feed = SyndicationFeed.Load(xmlReader);
                        }
                        else if (rss.CanRead(xmlReader))
                        {
                            _feed = SyndicationFeed.Load(xmlReader);
                        }
                        xmlReader.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }

答案 1 :(得分:0)

对我来说,解决方案在这个解决方案中起作用。

listing = Listing.objects.values()
if not listing.exists():
    listing = [{
        'address': 'some string',
        'range': 'some other string'
    }]

也许不是最好的方法,但它确实有效。非常感谢Alexandr对我的问题的提示和解决方案。