Solved

LINQ to XML - optional element

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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