简体   繁体   English

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

[英]SyndicationFeed - can't read url, error row and position

im having some problems with SyndicationFeed that is throwing an exception that i can't figure out, have been searching for an answer. 我在SyndicationFeed上遇到了一些问题,该问题引发了一个我无法弄清的异常,一直在寻找答案。 have tried most of the solutions that other users have suggested, but no luck. 尝试了其他用户建议的大多数解决方案,但没有运气。 here is the code. 这是代码。

// 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 i really need a hit of some sort. 类似的问题,我真的需要某种打击。

many thanks in advance. 提前谢谢了。

The problem is in cache node, remove it before parsing. 问题出在缓存节点中,请在解析之前将其删除。

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;
            }

the answer that work in this solution, for me. 对我来说,此解决方案有效的答案。

public XmlReader CleanXdocument(XmlReader xmlReader)
        {
            var xdoc = new XDocument();
            xdoc = XDocument.Load(xmlReader);
            if (xdoc.Descendants("cache").Any())
            {
                xdoc.Descendants().FirstOrDefault(e => e.Name == "cache").Remove();
            }
            var str = xdoc.ToString();
            TextReader tr = new StringReader(str);
            xmlReader =XmlReader.Create(tr);

            return xmlReader;
        }

maybe not the best way to do it, but it works. 也许不是最好的方法,但是它可以工作。 many thanks to Alexandr for the hint and solution to my problem. 非常感谢亚历山大为我的问题提供的提示和解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM