Yupi Yupi - 3 months ago 19
Android Question

Xmlpullparser in Xamarin android

I'm trying to parse rss feed from one website but I got stuck with parsing images. I want to replace my placeholders with images from that website and when I try to do that nothing happens my placeholders ar still there and here is my code from FeedService class:

try
{
WebRequest webRequest = WebRequest.Create(url);
WebResponse webResponse = webRequest.GetResponse();

Stream stream = webResponse.GetResponseStream();
XmlDocument xmlDocument = new XmlDocument();

xmlDocument.Load(stream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);
nsmgr.AddNamespace("dc", xmlDocument.DocumentElement.GetNamespaceOfPrefix("dc"));
nsmgr.AddNamespace("content", xmlDocument.DocumentElement.GetNamespaceOfPrefix("content"));

XmlNodeList itemNodes = xmlDocument.SelectNodes("rss/channel/item");

for (int i = 0; i < itemNodes.Count; i++)
{
FeedItem feedItem = new FeedItem();

if (itemNodes[i].SelectSingleNode("title") != null)
{
feedItem.Title = itemNodes[i].SelectSingleNode("title").InnerText;
}
if (itemNodes[i].SelectSingleNode("image") != null)
{
feedItem.Title = itemNodes[i].SelectSingleNode("image").InnerText;
}

if (itemNodes[i].SelectSingleNode("link") != null)
{
feedItem.Link = itemNodes[i].SelectSingleNode("link").InnerText;
}

if (itemNodes[i].SelectSingleNode("pubDate") != null)
{
feedItem.PubDate = Convert.ToDateTime(itemNodes[i].SelectSingleNode("pubDate").InnerText);
}

if (itemNodes[i].SelectSingleNode("dc:creator", nsmgr) != null)
{
feedItem.Creator = itemNodes[i].SelectSingleNode("dc:creator", nsmgr).InnerText;
}

if (itemNodes[i].SelectSingleNode("category") != null)
{
feedItem.Category = itemNodes[i].SelectSingleNode("category").InnerText;
}

if (itemNodes[i].SelectSingleNode("description") != null)
{
feedItem.Description = itemNodes[i].SelectSingleNode("description").InnerText;
}

if (itemNodes[i].SelectSingleNode("content:encoded", nsmgr) != null)
{
feedItem.Content = itemNodes[i].SelectSingleNode("content:encoded", nsmgr).InnerText;
}
else
{
feedItem.Content = feedItem.Description;
}

feedItemsList.Add(feedItem);
}
}
catch (Exception)
{
throw;
}

return feedItemsList;
}
}
}


and this is the code from FeedItemListAdapter class where I tried to replace placeholders with images from the website:

public override View GetView(int position, View convertView, ViewGroup parent)
{
var feedItem = this.feedsList[position];

var view = (convertView ?? context.LayoutInflater.Inflate(Resource.Layout.FeedItemListItemLayout, parent, false)) as LinearLayout;

view.FindViewById<TextView>(Resource.Id.title).Text = feedItem.Title.Length < 51 ? feedItem.Title : feedItem.Title.Substring(0, 50) + "...";
view.FindViewById<TextView>(Resource.Id.creator).Text = feedItem.Creator;
view.FindViewById<TextView>(Resource.Id.pubDate).Text = feedItem.PubDate.ToString("dd/MM/yyyy HH:mm");

ImageView imageView = view.FindViewById<ImageView>(Resource.Id.placeholder);
Koush.UrlImageViewHelper.SetUrlDrawable (imageView, feedItem.Image, Resource.Drawable.Placeholder);

return view;
}
}
}


and this is how it looks after I run it!

Can someone help?

Answer

That's probably because you have a small error in the code where you parse the RSS image node:

feedItem.Title = itemNodes[i].SelectSingleNode("image").InnerText;

It should be the Image not Title property you set:

feedItem.Image = itemNodes[i].SelectSingleNode("image").InnerText;