1
<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”或其他元素之前检查这里,因为如果不检查,会抛出空引用异常?

4

3 回答 3

3

不要使用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具有转换运算符来完成所有工作,返回适当的值。

于 2009-12-09T20:42:26.277 回答
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()
};
于 2009-12-09T20:42:20.047 回答
-1

只需更改行:

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

为了:

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

您可以根据需要更改 DateTime.Now

于 2009-12-09T20:46:31.677 回答