Quantcast
Channel: another test of atom feed read fails - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by Jon Skeet for another test of atom feed read fails

$
0
0

I strongly suspect that an exception is being thrown - but you can't tell because you're "handling" all exceptions by returning an empty list, without recording what the exception is or any kind of diagnostics. I would personally remove the try/catch altogether: if something is wrong, why hide that? If you must catch the exception, then catch specific exceptions, and log them so you know what's happened.

Your query can be simplified considerably, too:

XDocument doc = XDocument.Load("atom.xml");
XNamespace ns = "http://www.w3.org/2005/Atom";

var entries = doc.Root
                 .Elements(ns + "entry")
                 .Select(item => new Item
                 {
                      FeedType = FeedType.Atom,
                      Content = (string) item.Element(ns + "content"),
                      Link = (string) item.Element(ns + "link").Attribute("href"),
                      PublishDate = (DateTime) item.Element(ns + "published"),
                      Title = (string) item.Element(ns + "title")
                  };

Note that this will now set Content and Title to null if those elements don't exist; change to using .Value again if you would rather it threw an exception. You can get the same behaviour for PublishDate by casting to DateTime? instead of DateTime, obviously after changing the type of the property too. Handling absent links would be slightly harder, but not too bad - let me know if you want a hand there.

Basically I suspect it's an absent element - quite possibly published. It'll be easier to find out when you're logging (or not catching) exceptions though.

EDIT: Given your comment, it sounds like this has absolutely nothing to do with how you're handling the XML - it's loading the XML that's causing the problem. This is where not swallowing exceptions is important...

I don't know of any way to provide credentials when loading XML via XDocument.Load - you may well need to use WebRequest or WebClient to fetch the data, then parse it with XDocument.Load(Stream).


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images