Reputation: 415
I have written code in WPF c# to get RSS Atom XML Feed but it gives an Exception that root element id missing. How to solve this can you please help me. My code is:
try
{
string url = @"http://myweblink/newlink.xml";
string username = "";
string password = "";
Uri uri = new Uri(url);
HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(uri);
rssFeed.Method = "GET";
rssFeed.Credentials = new NetworkCredential(username, password);
using (DataSet rssData = new DataSet())
{
//read the xml from the stream of the web request
rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());
//loop through the rss items in the dataset
//and populate the list of rss feed items
foreach (DataRow dataRow in rssData.Tables["item"].Rows)
{
newlistt.Add(new RssFeedItem
{
ChannelId = Convert.ToInt32(dataRow["channel_Id"]),
Description = Convert.ToString(dataRow["description"]),
ItemId = Convert.ToInt32(dataRow["item_Id"]),
LinkURL = Convert.ToString(dataRow["link"]),
PublishDate = Convert.ToDateTime(dataRow["pubDate"]),
Title = Convert.ToString(dataRow["title"])
});
}
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
Upvotes: 0
Views: 1247
Reputation: 25623
Don't use DataSet
; it is very old and not intended for general-purpose XML reading. I recommend using LINQ to XML. Something like this:
var feed = XDocument.Load(rssFeed.GetResponse().GetResponseStream());
var ns = feed.Root.Name.Namespace;
var items = (from e in feed.Root.Elements(ns + "item")
select new RssFeedItem
{
ChannelId = (int?)e.Element(ns + "channel_Id") ?? -1,
Description = (string)e.Element(ns + "description"),
// ...
}).ToList();
Deal with missing values as you see fit.
Upvotes: 1