【问题标题】:c# LINQ to XML Query Expression for elemennt exhist?c# LINQ to XML 查询表达式元素是否存在?
【发布时间】:2012-02-06 08:49:47
【问题描述】:
<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>
        <pubDate>09 Dec 2009</pubDate>    
        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>   


<channel>
        <title>Best Web Gallery - Flash + CSS Gallery</title>
        <link>http://bestwebgallery.com</link>
        <description>Featuring the world best web design</description>

            // pubDate missing

        <generator>http://wordpress.org/?v=2.3.2</generator>
        <language>en</language>
</channel>




   XDocument rssFeed = XDocument.Load(url);

                    var feedResources = from details in rssFeed.Descendants("channel")
                                    select new feedResource
                                    {
                                         Title = details.Element("title").Value,
                                         Host = details.Element("link").Value,
                                         Description = details.Element("description").Value,  

                                         PublishedOn = DateTime.Parse(details.Element("pubDate").Value), 
                                         Generator = details.Element("generator").Value,
                                         Language = details.Element("language").Value
                                    };

我们如何在尝试获取元素“pubDate”或其他元素之前检查这里,因为如果不检查,会抛出空引用异常??

【问题讨论】:

    标签: c# linq-to-xml elements


    【解决方案1】:

    不要使用Parse等; xml 通常使用与其接受的不同的字符串表示形式;刚投(注意没有.Value):

    select new FeedResource
    {
        Title = (string)details.Element("title"),
        Host = (string)details.Element("link"),
        Description = (string)details.Element("description"),
        PublishedOn = (DateTime?)details.Element("pubDate"),
        Generator = (string)details.Element("generator"),
        Language = (string)details.Element("language")
    }
    

    XElement 具有转换运算符来完成所有工作,返回适当的值。

    【讨论】:

      【解决方案2】:

      我个人的偏好是给XElement添加两个扩展方法:

      public static string ValueOrDefault(this XElement xml)
      {
          if (xml == null) return null;   // or String.Empty, if you prefer
          return xml.Value
      }
      
      public static string ValueOrDefault(this XElement xml, string defaultValue)
      {
          if (xml == null) return defaultValue;
          return xml.Value
      }
      

      现在您的代码将如下所示:

      select new feedResource
      {
           Title = details.Element("title").ValueOrDefault(),
           Host = details.Element("link").ValueOrDefault(),
           Description = details.Element("description").ValueOrDefault(),  
      
           PublishedOn = DateTime.Parse(details.Element("pubDate").ValueOrDefault(DateTime.Now.ToString())), 
           Generator = details.Element("generator").ValueOrDefault(),
           Language = details.Element("language").ValueOrDefault()
      };
      

      【讨论】:

        【解决方案3】:

        只需换行:

        PublishedOn = DateTime.Parse(details.Element("pubDate").Value),
        

        为:

        PublishedOn = details.Element("pubDate") != null? DateTime.Parse(details.Element("pubDate").Value) : DateTime.Now,
        

        你可以随心所欲地改变 DateTime.Now

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多