我正在使用LINQ来读取某些XML,然后使用它来设置对象中的属性。 XML看起来像这样:
<display_location>
<full>London, United Kingdom</full>
<city>London</city>
<state/>
<state_name>United Kingdom</state_name>
<country>UK</country>
<country_iso3166>GB</country_iso3166>
<zip>00000</zip>
<magic>553</magic>
<wmo>03772</wmo>
<latitude>51.47999954</latitude>
<longitude>-0.44999999</longitude>
<elevation>24.00000000</elevation>
</display_location>
我的代码是:
select new Forecast
{
//Set properties for the display location
DisplayLatitude = (double)i.Element("display_location").Element("latitude"),
DisplayLongtitude = (double)i.Element("display_location").Element("longtitude"),
DisplayElevation = (string)i.Element("display_location").Element("elevation"),
};
我可以正确设置纬度和高程但是当我尝试解析经度时,我得到异常“值不能为空”。
我认为可能是因为负面符号。我该如何解决这个问题?
答案 0 :(得分:3)
这是“经度”,而非“经度”。您的字符串需要与XML元素名称完全匹配,否则您将获得空值。