Solved

LINQ to XML - optional element

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

22 Experts available now in Live!

Get 1:1 Help Now