Gps坐标(在C#中解析Xml文件) [英] Gps Coordinates(Parsing a Xml file in c#)

查看:102
本文介绍了Gps坐标(在C#中解析Xml文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个WebService方法.并且此方法将采用纬度",经度"和距离"参数.如果距离接近我们的节点,它将向我们显示最近的节点.
所以我写了一些东西,但我无法使它更清晰,更详细.你能帮我么??而且我也有一个XML文件.文件中有地址和分支...
谢谢!


I have to write a WebService Method. And this method going to take a Latitude, a Longitude and distance parameters. If the distance is close to our node, it is going to show us the nearest nodes.
So I wrote something but I cannot make it clear and more detailed. Can you please help me?? And also I have got a XML file too. There are addresses and Branches in the file ...
Thanks !


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
namespace Proje
{
    public class DistanceHelper
    {
        private double GetDistanceBetweenPoints(double lat1, double long1, double lat2, double long2)
        {
            double distance = 0;
            double dLat = (lat2 - lat1) / 180 * Math.PI;
            double dLong = (long2 - long1) / 180 * Math.PI;
            double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2)
                        + Math.Cos(lat2) * Math.Sin(dLong / 2) * Math.Sin(dLong / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

            double radiusE = 6378135;
            double radiusP = 6356750;

            double nr = Math.Pow(radiusE * radiusP * Math.Cos(lat1 / 180 * Math.PI), 2);
            double dr = Math.Pow(radiusE * Math.Cos(lat1 / 180 * Math.PI), 2)
                            + Math.Pow(radiusP * Math.Sin(lat1 / 180 * Math.PI), 2);
            double radius = Math.Sqrt(nr / dr);
            distance = radius * c;
            return distance;
        }

        public DataTable GetAppropriateBranches(int longtitude, int latitude, decimal distance)
        {
            DataTable dTable = new DataTable();
            XmlDocument xmlDoc = GetXmlDocument();
            DataTable returnValue = CreateDataTable();

            XmlNode mainNode = xmlDoc.ChildNodes[1];
            foreach (XmlNode childNode in mainNode.ChildNodes)
            {
                DataRow dRow = returnValue.NewRow();
                dRow["Adres"] = childNode.Attributes["Address"];
                dRow["Sube"] = childNode.Attributes["Branch"];
                returnValue.Rows.Add(dRow);
            }



            return dTable;
        }

        private DataTable CreateDataTable()
        {
            throw new NotImplementedException();
        }
        private XmlDocument GetXmlDocument()
        {
            throw new NotImplementedException();
        }
    }
}</pre>


--------------------------------------------------------------
<pre lang="cs">using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
namespace Proje
{
    /// &lt;summary&gt;
    /// Summary description for Service1
    /// &lt;/summary&gt;
    [WebService(Namespace = &quot;http://tempuri.org/&quot;)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebServicec
    {
        [WebMethod]
        public DataTable GetAppropriateBranches(int longtitude, int latitude, decimal distance)
        {
            return new DistanceHelper().GetAppropriateBranches(longtitude, latitude, distance);
        }
    }
}

推荐答案

是您的xml导航给您带来了麻烦吗?您链接的代码似乎能够确定距离并向响应表中添加新行,因此我猜测查询xml是您的问题.


如果是这样,请 System.Xml. XmlDocument.SelectNodes 是您需要的方法-您将需要学习基本的xpath.

另外,您可以搜索"linq to xml",并且有很多不错的文章介绍了如何使用linq查询xml.
Is it the navigation of your xml that''s causing you trouble? The code you''ve linked seems to be able to work out a distance and add new rows to your response table, so I''m guessing that querying your xml is your problem.


If that''s the case, then System.Xml.XmlDocument.SelectNodes is the method you need - and you''ll need to learn basic xpath.

Alternatively, you can search for "linq to xml" and there are plenty of good articles on how to query xml using linq.


这篇关于Gps坐标(在C#中解析Xml文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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