而阅读在C#中的文件意外结束GeoRSS订阅 [英] Unexpected end of file in C# while reading GeoRSS feed

查看:148
本文介绍了而阅读在C#中的文件意外结束GeoRSS订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节目,我取的GeoRSS从一个网站每10秒。只要没有新的项目添加到GeoRSS订阅,程序工作正常(即我可以读取并正确解析RSS - 还当现有的RSS项目的内容改变其值)。但是,一旦一个新的项目被添加到RSS源,我得到以下错误:

In my program I fetch GeoRSS from a website every 10th second. As long as no new items are added to the GeoRSS feed, the program works fine (i.e. I can fetch and parse the rss properly - also when the elements of the existing rss items change their value). However, as soon as a new item is added to the rss feed, I get the following error:

意外结束名称已经发生了。 85号线,13位

Unexpected end of file while parsing Name has occurred. Line 85, position 13.

堆栈跟踪:

       at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseQName(Boolean isQName, Int32 startOffset, Int32& colonPos)
   at System.Xml.XmlTextReaderImpl.ParseElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlReader.MoveToContent()
   at System.Xml.XmlReader.IsStartElement()
   at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadItemFrom(XmlReader reader, SyndicationItem result, Uri feedBaseUri)
   at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadItem(XmlReader reader, SyndicationFeed feed)
   at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadItems(XmlReader reader, SyndicationFeed feed, Boolean& areAllItemsRead)
   at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml(XmlReader reader, SyndicationFeed result)
   at System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFrom(XmlReader reader)
   at System.ServiceModel.Syndication.SyndicationFeed.Load[TSyndicationFeed](XmlReader reader)
   at Master.Model.ResourceManagerService.wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e) in C:\Users\polle\Documents\Visual Studio 2010\Projects\Master(23)\LSCommMaster\Master\Master\Services\ResourceManagerService.cs:line 319
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherOperation operation, CancellationToken cancellationToken, TimeSpan timeout)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()
   at Master.App.Main() in C:\Users\polle\Documents\Visual Studio 2010\Projects\Master(23)\LSCommMaster\Master\Master\obj\Debug\App.g.cs:line 0
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我用它来获取RSS的代码包含如下:

The code I use to fetch the rss is included below:

public static void FetchRSS(string url)
        {            

        if (url != String.Empty)
        {
            LoadRSS(url.Trim());
            DispatcherTimer UpdateTimer = new System.Windows.Threading.DispatcherTimer();
            UpdateTimer.Interval = new TimeSpan(0, 0, 0, 0, 10000);
            UpdateTimer.Tick += (evtsender, args) =>
            {
                LoadRSS(url.Trim());
            };
            UpdateTimer.Start();
        }
    }

    private static void LoadRSS(string uri)
    {
        Trace.WriteLine("Fetching rss feed");
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += wc_OpenReadCompleted;
        Uri feedUri = new Uri(uri, UriKind.Absolute);
        wc.OpenReadAsync(feedUri);
    }

    private static void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {

        if (e.Error != null)
        {
            Trace.WriteLine("Error in Reading Feed. Try Again later!");
            return;
        }

        using (Stream s = e.Result)
        {
            SyndicationFeed feed;
            List<SyndicationItem> feedItems = new List<SyndicationItem>();

            using (XmlReader reader = XmlReader.Create(s))
            {
                try
                {
                    feed = SyndicationFeed.Load(reader);
                    foreach (SyndicationItem feedItem in feed.Items)
                    {
                        SyndicationElementExtensionCollection ec = feedItem.ElementExtensions;

                        string title = feedItem.Title.Text;
                        string x = "";
                        string y = "";
                        string state = "not available";
                        string type = "not available";
                        string task = "not available";

                        foreach (SyndicationElementExtension ee in ec)
                        {
                            XmlReader xr = ee.GetReader();

                            switch (ee.OuterName)
                            {
                                case ("lat"):
                                    {
                                        y = xr.ReadElementContentAsString();
                                        break;
                                    }
                                case ("long"):
                                    {
                                        x = xr.ReadElementContentAsString();
                                        break;
                                    }
                                case ("point"):
                                    {
                                        string p = xr.ReadElementString();
                                        string[] coordinates = p.Split(' ');                                            
                                        y = coordinates[0];
                                        x = coordinates[1];
                                        break;
                                    }
                                case ("state"):
                                    {
                                        state = xr.ReadElementString();                                       
                                        break;
                                    }
                                case ("type"):
                                    {
                                        type = xr.ReadElementString();
                                        break;
                                    }
                                case ("taskDescription"):
                                    {
                                        task = xr.ReadElementString();
                                        break;
                                    }
                            }

                        }

                        if (!string.IsNullOrEmpty(x))
                        {
                            Resource resource = new Resource()
                            {
                                Geometry = new MapPoint(Convert.ToDouble(x, System.Globalization.CultureInfo.InvariantCulture),
                                Convert.ToDouble(y, System.Globalization.CultureInfo.InvariantCulture), new SpatialReference(4326))
                            };

                            resource.Attributes.Add("TITLE", title);
                            resource.Attributes.Add("STATE", state);
                            resource.Attributes.Add("TYPE", type);
                            resource.Attributes.Add("TASK", task);
                            resource.Attributes.Add("PENDINGTASK", "none");

                            resource.Title = title;
                            resource.TypeDescription = type;
                            resource.State = state;
                            resource.TaskDescription = task;
                            resource.PendingTask = "none";

                            ResourceDataReceivedMessage msg = new ResourceDataReceivedMessage() { Resource = resource };
                            Messenger.Default.Send<ResourceDataReceivedMessage>(msg);

                        }
                        else
                        {
                            Trace.WriteLine("STRING IS NULL OR EMPTY");
                        }
                    }
                }
                catch
                {
                    Trace.WriteLine("Exception occurred while fetching RSS feed" );
                }

            }
        }
    }

没有任何人有一个想法是什么导致了错误,我怎么能阻止它的发生?

Does anyone have an idea what's causing the error, and how I can prevent it from happening?

推荐答案

听起来像时你会得到更新你只是将它添加到你已经结束,可能外面错结束标记的?

sounds like when you get an update you are just adding it to the end of what you already have, possibly outside of the wrong ending tag?

<root>
    <!-- Tags Galore -->
</root>
<addedTags></addedTags>



像这样

Like that

这篇关于而阅读在C#中的文件意外结束GeoRSS订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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