Solved

LINQ to XML - optional element

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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