C# 如何在使用linqtoxml赋值之前检查元素的值是否不为null

C# 如何在使用linqtoxml赋值之前检查元素的值是否不为null,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我正在使用LINQtoXML,使用下面的代码从xml输入中读取值 var values = xDoc.Descendants("result").Select(c => new { FullAddress = c.Element("formatted_address").Value, CountryNam

我正在使用LINQtoXML,使用下面的代码从xml输入中读取值

var values = xDoc.Descendants("result").Select(c =>
                     new
                     {
                         FullAddress = c.Element("formatted_address").Value,

                          CountryName = c.Descendants("address_component")
                         .First(e => e.Element("type").Value == "country").Element("long_name").Value,

                         CountryNameCode = c.Descendants("address_component")
                         .First(e => e.Element("type").Value == "country").Element("short_name").Value,

                         CityName = c.Descendants("address_component")
                                 .First(e => e.Element("type").Value == "locality").Element("short_name").Value,


                     }).First();
我们如何检查xml输入中某个元素的值是否存在并且不为null,例如

 CityName = c.Descendants("address_component").First(e => e.Element("type").Value =="locality").Element("short_name").Value
我的输入xml是

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
    <type>postal_code</type>
    <formatted_address>Inverclyde PA16 0XN, UK</formatted_address>
    <address_component>
        <long_name>PA16 0XN</long_name>
        <short_name>PA16 0XN</short_name>
        <type>postal_code</type>
    </address_component>
    <address_component>
        <long_name>Inverclyde</long_name>
        <short_name>Inverclyde</short_name>
        <type>administrative_area_level_2</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>United Kingdom</long_name>
        <short_name>GB</short_name>
        <type>country</type>
        <type>political</type>
    </address_component>
    <geometry>
        <location>
            <lat>55.9437962</lat>
            <lng>-4.8097700</lng>
        </location>
        <location_type>APPROXIMATE</location_type>
        <viewport>
            <southwest>
                <lat>55.9426062</lat>
                <lng>-4.8127289</lng>
            </southwest>
            <northeast>
                <lat>55.9453041</lat>
                <lng>-4.8075776</lng>
            </northeast>
        </viewport>
        <bounds>
            <southwest>
                <lat>55.9428874</lat>
                <lng>-4.8127289</lng>
            </southwest>
            <northeast>
                <lat>55.9450229</lat>
                <lng>-4.8075776</lng>
            </northeast>
        </bounds>
    </geometry>
</result>
</GeocodeResponse>

好啊
邮政编码
Inverclyde PA16 0XN,英国
PA16 0XN
PA16 0XN
邮政编码
因弗克莱德
因弗克莱德
行政区二级
政治的
大不列颠联合王国
国标
国家
政治的
55.9437962
-4.8097700
近似
55.9426062
-4.8127289
55.9453041
-4.8075776
55.9428874
-4.8127289
55.9450229
-4.8075776

我认为扩展方法是处理这个问题的好方法。它需要一些检查,因为有很多时候一个值可能是空的,但这应该涵盖您的用例

void Main()
{
    var doc = XDocument.Load(@"C:\Test\test.xml");

    var city = doc.Descendants("address_component").FirstOrDefault(e => e.Element("type") != null 
                            && e.Element("type").Value =="locality");

    if(city != null){                       
     Console.WriteLine (city.GetElementIfExists("short_name"));
    } else {
     Console.WriteLine ("No short name found");
    }
}

public static class Extensions {
 public static string GetElementIfExists(this XElement @this, string element){
   return @this.Element(element) != null ? @this.Element(element).Value : String.Empty;
 }
}

谢谢@Jeroen。如果为空,我们如何将空字符串分配给CityName?@Learner:我不确定是否可以在LINQ查询中完成,但您可以始终将
CityName=CityName??字符串。空在它之后。我将用一个链接更新我的答案。请查看XML文档,在做了这些更改后,我在解析过程中仍然遇到错误。@Learner:我已经用一些应该有效的东西更新了我的帖子。