Here is the XML I am querying:
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>264 Mugga Ln, Canberra ACT, Australia</formatted_address>
<address_component>
<long_name>264</long_name>
<short_name>264</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Mugga Lane</long_name>
<short_name>Mugga Ln</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Canberra</long_name>
<short_name>Canberra</short_name>
<type>colloquial_area</type>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Australian Capital Territory</long_name>
<short_name>ACT</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
</result>
</GeocodeResponse>
And here is my C# code:
XElement localityname = xml.Element("result").Elements("address_component")
.Where(a => a.Element("type").Value == "locality")
.First().Element("long_name");
In most cases, the same/similar XML works fine and I get a value from my query.
What I'm looking for (and works most of the time) is the word "Canberra".
But why does this XML crash with "Sequence contains no elements"???