反复从网站XML中读取信息 [英] Repeatedly read info from a website XML

查看:52
本文介绍了反复从网站XML中读取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上,我要走到一个从XML页面给出公共汽车坐标的网页来跟踪公共汽车的位置.

我希望公交车能够自动更新其在地图上的位置.目前,它将更新约3次,然后才能停止工作.我已经使用调试器来检查代码,它会更新3次以上,所以我不确定会发生什么 上.另外,如果有更好的方法可以做到这一点,我全都听! :)

以下是进入网页并更新公交车位置的代码

//从网站获取xml数据并放置circ标记
        私有静态无效HttpCompleted(对象发送者,Dow​​nloadStringCompletedEventArgs e)
        {
            如果(e.Error == null)
            {
                字符串结果=(字符串)e.Result;
                XElement根= XElement.Parse(e.Result);
                var lat = root.Elements("markers").Elements("marker").Elements("lat").First().Value;
                var long1 = root.Elements("markers").Elements("marker").Elements("lng").First().Value;

                位置CircLoc = new Location()
                {
                    纬度= double.Parse(lat),
                    经度= double.Parse(long1)
                };

                MapLayer.SetPosition(pinCirc,CircLoc);
                
                //如果自动更新总线打开
                如果(getSetting("autoBusTrack"))
                {
                    //如果未按下刷新按钮
                    如果(!refreshHit)
                    {
                        int k = 0;
                        而(k <100)
                        {
                            k ++;
                        }
                        getCurrentCircLocation();
                    }
                }
                如果(refreshHit)
                {
                    refreshHit = false;
                }
            }
        }

        //获取当前位置
        公共静态无效getCurrentCircLocation()
        {
            WebClient wc =新的WebClient();
            wc.DownloadStringCompleted + = HttpCompleted;
            CircURL + =?v =" +数;
            wc.DownloadStringAsync(new Uri(CircURL));
            数++;
        } 

我在CircURL上添加了一个任意数字,因为在缓存网站之前,我一直在获取相同的数据.

解决方案

您好!

我可以看到一些可能导致此问题的东西:

1. CircURL-您要添加?v =" +数;对于u每次执行该方法,其倍增次数(在这种情况下为3 x?v = 1?v = 2?v = 3")肯定不好,但不会引起问题,您对此很感兴趣. /p>

2.您正在尝试在短时间内向网络服务器发送多个请求的垃圾邮件,也许在3个请求之后,网络服务器仅阻止了通信,并且您收到非xml响应-尝试添加一些try {} catch来对其进行检查

3.检查是否调用了"GetCurrentCircLocation()". HttpCompleted中的位置很好.



So basically I'm tracking the location of a bus, by going out to a webpage where the bus's coordinates are given from an XML page.

I want to be able to have the bus auto-update its location on the map. Right now, it will update for about 3 times before it stops working. I've used the debugger to go through the code and it would update more than 3 times, so I'm not sure what's going on. Also, if there's a better way to do this, I'm all ears! :)

Here's the bit of code that goes to the webpage and updates the bus's location

        //gets the xml data from the website and places the circ marker
        private static void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string result = (string)e.Result;
                XElement root = XElement.Parse(e.Result);
                var lat = root.Elements("markers").Elements("marker").Elements("lat").First().Value;
                var long1 = root.Elements("markers").Elements("marker").Elements("lng").First().Value;

                Location CircLoc = new Location()
                {
                    Latitude = double.Parse(lat),
                    Longitude = double.Parse(long1)
                };

                MapLayer.SetPosition(pinCirc, CircLoc);
                
                //if auto update bus is on
                if (getSetting("autoBusTrack"))
                {
                    //if the refresh button was not hit
                    if (!refreshHit)
                    {
                        int k = 0;
                        while (k < 100)
                        {
                            k++;
                        }
                        getCurrentCircLocation();
                    }
                }
                if (refreshHit)
                {
                    refreshHit = false;
                }
            }
        }

        // get current location
        public static void getCurrentCircLocation()
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += HttpCompleted;
            CircURL += "?v=" + count;
            wc.DownloadStringAsync(new Uri(CircURL));
            count++;
        }

I added an arbitrary number to the CircURL because before the website was getting cached, and I kept getting the same data.

解决方案

Hi there !

I can see a few things that might cause this problem:

1. CircURL - you are adding that "?v=" + count; for every time u execute that method, so its multiplied many times (in this case 3 x "?v=1?v=2?v=3") that's bad for sure, but prolly not causing issue u are interested.

2. You are trying to spam a webserver with multiple requests in a short period of time, maybe after 3 requests webserver just blocks communication and you get non xml response - try to add some try {} catch to check it

3. Check if that call of "GetCurrentCircLocation()" in HttpCompleted is in good place.



这篇关于反复从网站XML中读取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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