I am using this function public static
IList<Item> ParseAtom(string url)
{
try
{
XDocument doc = XDocument.Load(url);
// Feed/Entry
var entries = from item in doc.Root.Elements().Where(i => i.Name.LocalName == "entry")
select new Item
{
FeedType = FeedType.Atom,
Content = item.Elements().First(i => i.Name.LocalName == "content").Value,
Link = item.Elements().First(i => i.Name.LocalName == "link").Attribute("href").Value,
PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "published").Value),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value
};
return entries.ToList();
}
catch
{
return new List<Item>();
}
}
I am using the following link
http://localhost/posts.atom/
When i open that link in the browser I see the list of links that direct me to each post item.
- 1 Link 2 Link 3 Link 4 Link
but I when I use it as a url to parse, my program return nothing. I suppose myself that I don’t understand what the link above actually means. I really need any of your help to offer me somemore explanation of atom feed, thank you very much .
The XML is created by another file that is stored as a stream as in the following format
<Root>
<Child id=123456>
<Child1>Child1
</Child1>
<Child2>Child2
</Child2>
</Child>
</Root>
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:
Note that this will now set Content and Title to null if those elements don’t exist; change to using
.Valueagain if you would rather it threw an exception. You can get the same behaviour forPublishDateby casting toDateTime?instead ofDateTime, 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 useWebRequestorWebClientto fetch the data, then parse it withXDocument.Load(Stream).