XML C# - trying to get a List of elements from an xml document

Akhil

New Member
I am still learning to work with XML and C#.I have looked many places on how to get this to work properly but I am unable to solve this as of yet and was wondering if anyone can see where I am going wrong?I am trying to get a list containing the node values for distance and duration for two seperate occasions. First should be just one pair which is the total dist/duration pair: /DirectionsResponse/route/leg/distance/value, then I'm trying to get a second list which will contain the steps version: /DirectionsResponse/route/leg/steps/distance/value. If I can get the second one working I can figure out the first.Many ThanksJaie\[code\]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(); }}\[/code\]
 
Back
Top