TheSwamp

Code Red => .NET => Topic started by: FELIX on October 31, 2020, 07:55:39 AM

Title: HTTPS request and read XML
Post by: FELIX on October 31, 2020, 07:55:39 AM
How to do in VB.NET execute the following request and then obtain the elevation value in the returned XML?

"https://maps.googleapis.com/maps/api/elevation/xml?locations=-3.60,-45.35&key=AIzaSyCnQ2jPnu0bhyDwJ8vMq5u4untaiEu5ma4"

<ElevationResponse>
<status>OK</status>
<result>
<location>
<lat>-3.6000000</lat>
<lng>-45.3500000</lng>
</location>
<elevation>6.3086224</elevation>
<resolution>152.7032318</resolution>
</result>
</ElevationResponse>

ok?
Title: Re: HTTPS request and read XML
Post by: n.yuan on October 31, 2020, 08:52:03 AM
There are a number of ways to get data from http response in the format of XML to deserialize the response string into an object of a class. Here are a couple of them:

1. use XmlSerializer. See this question and answer from StackOverFlow:

https://stackoverflow.com/questions/19942486/how-to-use-httpclient-to-read-an-xml-response

2. use XDocument.Parse() to convert the response string into a XmlDocument, then do whatever with the XmlDocument. See this question and answer

https://forums.asp.net/t/2160077.aspx?How+To+Parse+XML+Response+From+API

BTW, you can google for "HttpClient returns XML", which would bring you tons of links.
Title: Re: HTTPS request and read XML
Post by: MickD on October 31, 2020, 02:39:59 PM
There are many ways to get data from the web and after retrieving the data using serialisation may be overkill so using XPath is a simple way of obtaining elements from XML, as this is a simple document you could simply use:

//elevation

but this can give you a list of all nodes of tag 'elevation' and there are ways to single out certain tags using criteria.
https://www.w3schools.com/xml/xpath_syntax.asp

hth