Solved

LINQ to XML - optional element

Posted on 2008-10-23
2
709 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now