Solved

LINQ to XML - optional element

Posted on 2008-10-23
2
716 Views
Last Modified: 2013-11-11
Hi

I've got the code below which queries an RSS feed which works fine but I've just come across a feed in which the pubDate element is optional and doesn't occur in all items.

What do I need to do to make this optional in the code below in both the Order By and Select. I've done some Googling but can't find how to do this.

Thanks

Chris
Dim rssFeed As XDocument = XDocument.Load(strURL)
        Dim items = From item In rssFeed.Descendants("channel").Descendants("item") _
                    Order By CType(item.Element("pubDate").Value, DateTime) Descending _
                    Select title = item.Element("title").Value, _
                    link = item.Element("link").Value, _
                    description = item.Element("description").Value, _
                    publishedDate = item.Element("pubDate").Value

Open in new window

0
Comment
Question by:cmhunty
2 Comments
 
LVL 11

Accepted Solution

by:
Salim Fayad earned 500 total points
ID: 22797519
Here is what you have to do regarding the publishedDate:

                    publishedDate = IIf(IsNothing(item.Element("pubDate").Value), "", item.Element("pubDate").Value)

Open in new window

0
 
LVL 11

Author Closing Comment

by:cmhunty
ID: 31509209
Perfect, thank you.

Cheers

Chrs
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question