RSS will see a small customer in the form of console application that uses LINQ. NET to parse the xml file in an elegant and concise.
First, we define Channel and Item classes, which will shape the objects of an RSS document:
Channel class
{
public string Title {get; set;}
public string Link {get; set;}
public string Description {get; set;}
public IEnumerable <Item> Items {get; set;}
}
{
public string Title {get; set;}
public string Link {get; set;}
public string Description {get; set;}
public IEnumerable <Item> Items {get; set;}
}
Item class
{
public string Title {get; set;}
public string Link {get; set;}
public string Description {get; set;}
public string Guid {get; set;}
}
{
public string Title {get; set;}
public string Link {get; set;}
public string Description {get; set;}
public string Guid {get; set;}
}
Then we create the method that gets (using LINQ) the list of items from the feed Channels:
static IEnumerable <Channel> getChannelQuery (XDocument xDoc)
{
xDoc from channels in return. Descendants ("channel")
select new Channel
{
Title = channels. Element ("title")! = Null? channels. Element ("title"). Value: "",
Link = channels. Element ("link")! = Null? channels. Element ("link"). Value: "",
Description = channels. Element ("description")! = Null? channels. Element ("description"). Value: "",
Items = from items in channels. Descendants ("item")
select new Item
{
Title = items. Element ("title")! = Null? items. Element ("title"). Value: "",
Link = items. Element ("link")! = Null? items. Element ("link"). Value: "",
Description = items. Element ("description")! = Null? items. Element ("description"). Value: "",
Guid = (items. Element ("guid")! = Null? Items. Element ("guid"). Value: "")
}
};
}
{
xDoc from channels in return. Descendants ("channel")
select new Channel
{
Title = channels. Element ("title")! = Null? channels. Element ("title"). Value: "",
Link = channels. Element ("link")! = Null? channels. Element ("link"). Value: "",
Description = channels. Element ("description")! = Null? channels. Element ("description"). Value: "",
Items = from items in channels. Descendants ("item")
select new Item
{
Title = items. Element ("title")! = Null? items. Element ("title"). Value: "",
Link = items. Element ("link")! = Null? items. Element ("link"). Value: "",
Description = items. Element ("description")! = Null? items. Element ("description"). Value: "",
Guid = (items. Element ("guid")! = Null? Items. Element ("guid"). Value: "")
}
};
}
Finally, we ask the user to the feed path to view and submit entries to the console:
static void Main ()
{
Console. WriteLine ("Customer RSS. NET LINQ");
Console. Write ("Enter the RSS feed URL:");
FeedUri String = Console. In. ReadLine ();
{
Console. WriteLine ("Customer RSS. NET LINQ");
Console. Write ("Enter the RSS feed URL:");
FeedUri String = Console. In. ReadLine ();
myFeed var =
foreach (var item in myFeed)
{
Console. WriteLine ("{0} - {1}", item. Title, item. Description);
foreach (var item in myFeed)
{
Console. WriteLine ("{0} - {1}", item. Title, item. Description);
foreach (var i in item. Items)
{
Console. WriteLine ("{0}", i. Title);
}
}
Console. WriteLine ("Press any key to continue ...");
Console. ReadKey ();
}
{
Console. WriteLine ("{0}", i. Title);
}
}
Console. WriteLine ("Press any key to continue ...");
Console. ReadKey ();
}
0 comments:
Post a Comment