LINQ .ToList() fails on single result
It could be that its late Friday evening and I want to go home, or more likely I just can't see the problem...
I have the fallowing LINQ code:
mthumbs = itm.Elements(nsr + "thumbnail") != null ?
(from mt in itm.Elements(nsr + "thumbnail")
select new MediaThumbnail
{
mediaThumbnailUrl = (string)mt.Attribute("url") ?? null,
mediaThumbnailHeight = (string)mt.Attribute("height") ?? null,
mediaThumbnailWidth = (string)mt.Attribute("width") ?? null,
mediaThumbnailTime = (string)mt.Attribute("time").Value ?? null
}).ToList() : null
The MediaThumbnail Class looks like:
public class MediaThumbnail
{
public string mediaThumbnailUrl { get; set; }
public string mediaThumbnailWidth { get; set; }
public string mediaThumbnailHeight { get; set; }
public string mediaThumbnailTime { get; set; }
}
and resides in the MediaOptElement class which looks like:
public class MediaOptElement
{
public string mediaPlayer { get; set; }
public string mediaRating { get; set; }
public string mediaRatingScheme { get; set; }
public string mediaTitle { get; set; }
public string mediaTitleType { get; set; }
public string 开发者_运维百科mediaDescription { get; set; }
public string mediaDescriptionType { get; set; }
public string mediaKeywords { get; set; }
public List<MediaThumbnail> mthumbs { get; set; }
public string mediaCategory { get; set; }
public string mediaCategoryScheme { get; set; }
public string mediaCategoryLabel { get; set; }
public string mediaHash { get; set; }
public string mediaHashAlgorithm { get; set; }
public string mediaCopyRights { get; set; }
public string mediaCopyrightsUrl { get; set; }
public string mediaRestrictions { get; set; }
public string mediaRestrictionsRelation { get; set; }
public string mediaRestrictionsType { get; set; }
public List<MediaCredit> mediaCredit { get; set; }
}
Some Sample XML:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>wdbj7.com - Sports</title>
<link>http://www.wdbj7.com/sports/?track=rss</link>
<description>Headlines from wdbj7.com</description>
<language>en</language>
<copyright>©2011, wdbj7.com</copyright>
<lastBuildDate>Fri, 21 Jan 2011 10:47:00 -0500</lastBuildDate>
<item>
<title>Brookville improves to 15-0</title>
<link>http://www.wdbj7.com/sports/wdbj7-brookville-improves-to-150-01212011,0,675989.story?track=rss</link>
<description><p>The Brookville girls' basketball team graduated two college basketball players last year. That was the last time they lost anything.</p> <p>The Lady Bees are 14-0 on the young season and looked to add to that mark Thursday night against Seminole district rival Liberty.</p> <p>The Minettes playing at home, had just three wins on the year. Haley Comer gets two for the home team. Liberty takes the 2-0 lead. That is when it becomes the Katie Deacon show.</p> <p>The four-year varsity player starts it with the defense. She gets the steal on one end and a basket on the other.</p> <p>Deacon takes her game behind the three point arc! That beats the buzzer at the end of the first quarter!</p> <p>Deacon still not done. She gets another steal and feeds George Mason signee Talisha Watts.<br /><br />Brookville improves to 15-0 with the 57-34 win. <br /><br /></p></description>
<pubDate>Fri, 21 Jan 2011 10:47:00 -0500</pubDate>
<media:thumbnail url="http://media.trb.com/media/thumbnails/story/2011-01/276990840-21074740.jpg" />
<media:content url="http://media.trb.com/media/alternatethumbnails/story/2011-01/276990840-21074739.jpg" />
</item>
</channel>
</rss>
When I run my code, I get the following error:
Object reference not set to an instance of an object.
The stack trace is:
at RssAggregator.Reader.RssReader.<>c__DisplayClass7d.<parseFeed>b__6e(XElement mt) in E:\Aggregator\VersionControl\FeedsParserLINQ\RssAggregator\Reader \RssReader.cs:line 843
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at RssAggregator.Reader.RssReader.<>c__DisplayClass7d.<parseFeed>b__57(XElement itm) in E:\Aggregator\VersionControl\FeedsParserLINQ\RssAggregator\Reader\RssReader.cs:line 667
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at RssAggregator.Reader.RssReader.parseFeed(XDocument xmlDoc) in E:\Aggregator\VersionControl\FeedsParserLINQ\RssAggregator\Reader\RssReader.cs:line 666
I could be wrong, but I think the problem happens when the LINQ query only returns a single result. However, I need to be able to handle cases when there might only be one media:thumbnail node or many media:thumbnail nodes. Any ideas?! Thanks!
I think the problem is in this line:
mediaThumbnailTime = (string)mt.Attribute("time").Value ?? null
If the attribute doesn't exist, then you'll get a NullReferenceException
when you attempt to use the Value
attribute. Note in your example the time
attribute isn't present.
Try this:
...
using System.Linq;
using System.Xml.Linq;
var mthumbs = itm.Elements( nsr + "thumbnail" )
.Select( e => new MediaThumbnail
{
mediaThumbnailUrl = string.Format( "{0}", e.Attribute("url" ),
mediaThumbnailHeight = string.Format( "{0}",e.Attribute("height" ),
mediaThumbnailWidth = string.Format( "{0}",e.Attribute("width" ),
mediaThumbnailTime = string.Format( "{0}", e.Attribute("time" )
})
.ToList();
I would look at his line:
mediaThumbnailTime = (string)mt.Attribute("time").Value ?? null
My guess is that mt.Attribute("time")
is null, and that's what's causing the exception. It's hard to tell for sure from the information we have. Perhaps something like:
mediaThumbnailTime = (mt.Attribute("time") != null ? (string)mt.Attribute("time").Value : null)
Your problem is in the following line of code:
mediaThumbnailTime = (string)mt.Attribute("time").Value ?? null
Note that it differs from the other 3 in that you use .Value
here. This fails if the element does not have attribute "time" - in this case .Attribute()
will return null
, and accessing a property on that gives you NullReferenceException
.
精彩评论