I’m trying to read this RSS feed using SyndicationFeed. I’m encountering this particular element that I can’t find a way to read:
<image>
<url>https://images.igdb.com/igdb/image/upload/t_cover_big/co25po.jpg</url>
</image>
I think image is a standard element of RSS (I might be wrong), but it’s only showing up in ElementExtensions. The problem here is that it doesn’t have a custom namespace, so it shows up like this:
So I’m using the following to read the ElementExtension like so, with the name "image" but no namespace name provided (I’ve tried both empty string and null for the second argument):
public static string? ReadElementExtension(this SyndicationItem item, string extensionName, string extensionNamespace)
{
var elementExtensions = item.ElementExtensions
.ReadElementExtensions<string>(extensionName, null); //NOTE: I pass in "image" for extensionName here
return elementExtensions.FirstOrDefault();
}
I get the following SerializationException error from ReadElementExtensions:
‘There was an error deserializing the object of type System.String. End element ‘image’ from namespace ” expected. Found element ‘url’ from namespace ”.’
I also tried using the name "url" for this element but it doesn’t find anything that way. So I’m wondering if there’s something wrong with how my SyndicationFeed is set up and if I should be able to more easily access the image. Here’s the full code that calls the above method:
var reader = XmlReader.Create(_backloggdSettings.BackloggdProfileUrl);
var feed = SyndicationFeed.Load(reader);
var feedItems = limit > 0 ? feed.Items.Take(limit) : feed.Items;
var items = feedItems.Select(i =>
{
var imageUrl = i.ReadElementExtension("image","");
var userRating = i.ReadElementExtension("user_rating", BackloggdExtensionNamespace);
var link = i.Links.FirstOrDefault()?.Uri.AbsoluteUri;
return new BackloggdItem
{
Title = i.Title.Text,
Summary = i.Summary.Text,
Url = link,
Rating = userRating != null ? Convert.ToDecimal(userRating) : null,
ImageUrl = imageUrl,
};
});
return items;
>Solution :
It seems that the code is trying to deserialize the "image" element as a string, which is causing the error. Instead, you can try to access the "url" element directly using the SyndicationItem.ElementExtensions property, like this:
var imageUrl = i.ElementExtensions
.Where(e => e.OuterName == "image" && e.OuterNamespace == "")
.Select(e => e.GetObject<XmlElement>().SelectSingleNode("url")?.InnerText)
.FirstOrDefault();
This code looks for any "image" elements in the default namespace (no namespace), and then selects the "url" element inside it and returns its inner text (the image URL). If there are multiple "image" elements, it selects the first one.
