asp.net - Reading Value From Google Distance Matrix API C# -
i have following xml packet comes google.
<?xml version="1.0" encoding="utf-8" ?> - <distancematrixresponse> <status>ok</status> <origin_address>poplar drive, kingsbridge, devon tq7 1sf, uk</origin_address> <destination_address>1 saint michaels road, kingsteignton, newton abbot, devon tq12 3aq, uk</destination_address> - <row> - <element> <status>ok</status> - <duration> <value>2710</value> <text>45 mins</text> </duration> - <distance> <value>37958</value> <text>38.0 km</text> </distance> </element> </row> </distancematrixresponse>
now trying assign 2 values fields variables.
i have pulled stream ie.
stream stream = client.openread("http://ho-www/googleapiaccess/distancematrix.aspx?origins=" + orilat + "," + orilong + "&destinations=" + destlat + "," + destlong + "&mode=driving'&units=imperial'&language=en'&sensor=false' (http://ho-www/googleapiaccess/distancematrix.aspx?origins=" + orilat + "," + orilong + "&destinations=" + destlat + "," + destlong + "&mode=driving%27&units=imperial%27&language=en%27&sensor=false%27)");
everything have searched far tells me how parent elements ie duration , distance not how underneath.
this should simple answer cant think through...
can put me out misery!
there several ways of doing asked, , don't know way you're more familiar with. that's why asked post codes have @ moment.
the other answer demonstrates 1 way using xmldocument
, xpath selector syntax. possible way using linq-to-xml's xdocument
, method chain syntax :
var doc = xdocument.load(stream); var duration = (string)doc.root .element("row") .element("element") .element("duration") .element("value"); var distance = (string)doc.root .element("row") .element("element") .element("distance") .element("value");
Comments
Post a Comment