XML C# - 尝试从xml文档中获取元素列表

时间:2013-03-30 09:13:04

标签: c# asp.net xml linq-to-xml

我仍在学习使用XML和C#。

我已经看了很多关于如何使其正常工作的地方,但我还是无法解决这个问题,并且想知道是否有人能看到我哪里出错了? 我试图得到一个列表,其中包含两个单独场合的距离和持续时间的节点值。首先应该只是一对,即总的dist / duration对:/ DirectionsResponse / route / leg / distance / value,然后我试图获得第二个列表,其中包含步骤版本:/ DirectionsResponse / route / leg /步骤/距离/值。如果我能让第二个工作,我可以弄清楚第一个。

非常感谢 Jaie

public class MyNode
    {
        public string Distance { get; set; }
        public string Duration { get; set; }        
    }

public class Program
{
    static void Main(string[] args)
    {
        //The full URI
        //http://maps.googleapis.com/maps/api/directions/xml?`enter code here`origin=Sydney+australia&destination=Melbourne+Australia&sensor=false

        //refer: https://developers.google.com/maps/documentation/webservices/
        string originAddress = "Canberra+Australia";
        string destinationAddress = "sydney+Australia";
        StringBuilder url = new StringBuilder();
        //http://maps.googleapis.com/maps/api/directions/xml?
        //different request format to distance API
        url.Append("http://maps.googleapis.com/maps/api/directions/xml?");
        url.Append(string.Format("origin={0}&", originAddress));
        url.Append(string.Format("destination={0}", destinationAddress));
        url.Append("&sensor=false&departure_time=1343605500&mode=driving");

        WebRequest request = HttpWebRequest.Create(url.ToString());

        var response = request.GetResponse();
        var stream = response.GetResponseStream();
        XDocument xdoc = XDocument.Load(stream);

        List<MyNode> routes =
            (from route in xdoc.Descendants("steps")
             select new MyNode
             {
                 Duration = route.Element("duration").Value,
                 Distance = route.Element("distance").Value,
             }).ToList<MyNode>();

        foreach (MyNode route in routes)
        {
            Console.WriteLine("Duration = {0}", route.Duration);
            Console.WriteLine("Distance = {0}", route.Distance);
        }

        stream.Dispose();

    }
}

1 个答案:

答案 0 :(得分:0)

我认为你非常接近你想要的地方,只需要一点调试就可以让你上线。

以下是我在LinqPad中汇总的一小部分内容,在我提交代码之前,我将其用于一起划分。

var origin = "Canberra+Australia";
var dest = "sydney+Australia";
var baseUrl = "http://maps.googleapis.com/maps/api/directions/xml?origin={0}&destination={1}&sensor=false&departure_time=1343605500&mode=driving";

var req = string.Format(baseUrl, origin, dest);
var resp = new System.Net.WebClient().DownloadString(req);
var doc = XDocument.Parse(resp);

var total = doc.Root.Element("route").Element("leg").Element("distance").Element("value").Value;
total.Dump();

var steps = (from row in doc.Root.Element("route").Element("leg").Elements("step")
             select new 
             { 
                Duration = row.Element("duration").Element("value").Value,
                Distance = row.Element("distance").Element("value").Value
             }).ToList();
steps.Dump();

Dump方法将结果吐出到LinqPad结果中。我在步骤结果中列出了16个项目,总距离值为286372。

希望这有帮助。