Select to view content in your preferred language

Unexpected end of file while reading rss feed

581
2
08-06-2012 01:36 PM
Labels (1)
ae
by
Frequent Contributor
Hi,

I'm reading an rss feed every 10th second, but on some occations I receive a Unexpexted end of file, although I'm 100% certain that the rss is valid. I'm using the sample code for ArcGIS API for WPF 2.4.

Does anyone have an idea of what the problem is, and how I can resolve it? The code is included below:

private void Fetch_Click(object sender, RoutedEventArgs e)
        {
            if (FeedLocationTextBox.Text != String.Empty)
            {
                LoadRSS(FeedLocationTextBox.Text.Trim());
                DispatcherTimer UpdateTimer = new System.Windows.Threading.DispatcherTimer();
                UpdateTimer.Interval = new TimeSpan(0, 0, 0, 0, 60000);
                UpdateTimer.Tick += (evtsender, args) =>
                {
                    LoadRSS(FeedLocationTextBox.Text.Trim());
                };
                UpdateTimer.Start();
            }
        }

        protected void LoadRSS(string uri)
        {
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += wc_OpenReadCompleted;
            Uri feedUri = new Uri(uri, UriKind.Absolute);
            wc.OpenReadAsync(feedUri);
        }

        private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {

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

            // Optional, use LINQ to query GeoRSS feed.
            //UseLinq(e.Result); return;

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

                GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.ClearGraphics();

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

                        string x = "";
                        string y = "";
                        string magnitude = feedItem.Title.Text;

                        foreach (SyndicationElementExtension ee in ec)
                        {
                            XmlReader xr = ee.GetReader();
                            switch (ee.OuterName)
                            {
                                case ("lat"):
                                    {
                                        y = xr.ReadElementContentAsString();
                                        break;
                                    }
                                case ("long"):
                                    {
                                        x = xr.ReadElementContentAsString();
                                        break;
                                    }
                            }
                        }

                        if (!string.IsNullOrEmpty(x))
                        {
                            Graphic graphic = new Graphic()
                            {
                              Geometry = new MapPoint(Convert.ToDouble(x, System.Globalization.CultureInfo.InvariantCulture),
                          Convert.ToDouble(y, System.Globalization.CultureInfo.InvariantCulture),
                                                      new SpatialReference(4326)),
                                Symbol = LayoutRoot.Resources["QuakePictureSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol
                            };

                            graphic.Attributes.Add("MAGNITUDE", magnitude);

                            graphicsLayer.Graphics.Add(graphic);
                        }
                    }
                }



Thanks.
0 Kudos
2 Replies
BKuiper
Frequent Contributor
only load the RSS again when the other load is finished. Now you are doing it every 10 seconds but the previous request might not yet have finished. Other issue i can think of is that the RSS feed file is updated while you are trying to retrieve it. You will need to send us the StackTrace.

Anyway, not really an Esri problem, but anyway, let's see if we can help you.
0 Kudos
ae
by
Frequent Contributor
The error only seems to occur when the rss has been updated. The stacktrace is included below:

System.Xml.XmlException was unhandled
  HResult=-2146232000
  Message=Unexpected end of file has occurred. The following elements are not closed: rss. Line 35, position 6.
  Source=System.Xml
  LineNumber=35
  LinePosition=6
  SourceUri=""
  StackTrace:
       at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
       at System.Xml.XmlTextReaderImpl.ParseEndElement()
       at System.Xml.XmlTextReaderImpl.ParseElementContent()
       at System.Xml.XmlReader.MoveToContent()
       at System.Xml.XmlReader.ReadEndElement()
       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.ViewModels.Map2dViewModel.wc_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e) in C:\Users\aslakw\Desktop\Master(9)\Master\ViewModels\Map2dViewModel.cs:line 326
       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\aslakw\Desktop\Master(9)\Master\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       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()
  InnerException: 
0 Kudos