Optimize Arraylist function call I am using the following to retrieve some user info based on IP address. ClientLocation() returns an array with city, state and coordinates. 

My concern with this is the function is called whenever I want to retrieve different array values. How can this be optimized so that the function is called once, but still retrieve multiple array values.

Public Function ClientLocation() As Object

    Dim Result As ArrayList = New ArrayList
    Dim XMLDoc As XmlDocument = New XmlDocument()
    Dim APIKey = "apikey"
    Dim GetXML = "url in xml"

    Try
        XMLDoc.Load(GetXML)
        Dim XMLResponse As XmlNodeList = XMLDoc.SelectNodes("Response")
        For Each Item As XmlNode In XMLResponse
            Result.Add(Item("cityName").InnerText &amp; ", " &amp; Item("regionName").InnerText)
            Result.Add(Item("latitude").InnerText &amp; "," &amp; Item("longitude").InnerText)
            Return (Result)
        Next
    Catch err As Exception
        Result.Add("Not Available")
        Result.Add("Not Available")
        Return (Result)
    End Try

    Return Nothing
End Function


Calling function

Dim UserProfile As ProfileCommon = Profile.GetProfile(User.Identity.Name)

UserProfile.Location = ClientLocation.Item(0)
UserProfile.Coordinates = ClientLocation.Item(1)
UserProfile.Save()
Assuming you have an xml structure that would be similar to:

&lt;Responses&gt;
    &lt;Response&gt;
        &lt;Item&gt;
            &lt;cityname&gt;abc&lt;/cityname&gt;
            &lt;regionname&gt;def&lt;/regionname&gt;
            &lt;latitude&gt;101.2345&lt;/latitude&gt;
            &lt;longitude&gt;34.3456&lt;/longitude&gt;
        &lt;/Item&gt;
        &lt;Item&gt;
            &lt;cityname&gt;ghi&lt;/cityname&gt;
            &lt;regionname&gt;jkl&lt;/regionname&gt;
            &lt;latitude&gt;101.2345&lt;/latitude&gt;
            &lt;longitude&gt;34.3456&lt;/longitude&gt;
        &lt;/Item&gt;
        &lt;Item&gt;
            &lt;cityname&gt;mno&lt;/cityname&gt;
            &lt;regionname&gt;pqr&lt;/regionname&gt;
            &lt;latitude&gt;101.2345&lt;/latitude&gt;
            &lt;longitude&gt;34.3456&lt;/longitude&gt;
        &lt;/Item&gt;
        &lt;Item&gt;
            &lt;cityname&gt;stu&lt;/cityname&gt;
            &lt;regionname&gt;vwx&lt;/regionname&gt;
            &lt;latitude&gt;101.2345&lt;/latitude&gt;
            &lt;longitude&gt;34.3456&lt;/longitude&gt;
        &lt;/Item&gt;
    &lt;/Response&gt;
&lt;/Responses&gt;


You could use the XMLSerializer class to deserialize the whole file all at once, by creating a class to contain the data:

Public Class Responses
    Public Class Item
        Dim cityname As String = ""
        Dim regionname As String = ""
        Dim latitude As Double = 0.0
        Dim longitude As Double = 0.0
    End Class
    &lt;XmlArrayAttribute("Response")&gt;
    Dim items As New List(Of Item)
End Class


Getting the data would be as simple as:

XMLDoc.Load(GetXML)
Dim result As Responses = CType(New XmlSerializer(GetType(Responses)) _
.Deserialize(New StringReader(XMLDoc.OuterXml)), Responses)


You would access the data through the items field of result:

result.items(0).cityname