I've extracted some addresses from google maps and they are in an xml file. In myxml file I have some xelements like
<location>, <place_id>, <adr_address>, etc
The 'adr_address' element has different classes and each class contains, city, street, country, etc. values. How do I get each value from the 'adr_address' xElement
<adr_address><span class="street-address">1805 Geary Boulevard</span>, <span class="locality">San Francisco</span>, <span class="region">CA</span> <span class="postal-code">94115</span>, <span class="country-name">United States</span></adr_address>
I'm putting the adr_address xElement in to a object here, but not sure what to do to get the values of each class after that.
XElement firstOrDefault = xElement.Descendants("adr_address").FirstOrDefault();
It seems strange to me that you get values like address, zip code, .. in this form. Normally Google Maps should give these values properly parsed.
Anyway, what you can do is unescape the special characters like this:
firstOrDefault.Value.Replace("<", "<").Replace(">", ">");
and then use this regular expression to extract the values:
var str = "<span class=\"street-address\">1805 Geary Boulevard</span>, <span class=\"locality\">San Francisco</span>, <span class=\"region\">CA</span> <span class=\"postal-code\">94115</span>, <span class=\"country-name\">United States</span>".Replace("<", "<").Replace(">", ">");
Regex regex = new Regex("<span class=\"street-address\">(.*)</span>, <span class=\"locality\">(.*)</span>, <span class=\"region\">(.*)</span> <span class=\"postal-code\">(.*)</span>, <span class=\"country-name\">(.*)</span>");
Match match = regex.Match(str);
if (match.Success)
{
string address = match.Groups[1].Value;
string locality = match.Groups[2].Value;
string region = match.Groups[3].Value;
string zip = match.Groups[4].Value;
string country = match.Groups[5].Value;
}