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
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[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.