C#
public ActionResult Index()
{
var request = WebRequest.Create("http://search.twitter.com/search.atom?q=worldcup&rpp=5") as HttpWebRequest;
var twitterViewData = new List<TwitterViewData>();
if (request != null)
{
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var document = XDocument.Parse(reader.ReadToEnd());
XNamespace xmlns = "http://www.w3.org/2005/Atom";
twitterViewData = (from entry in document.Descendants(xmlns + "entry")
select new TwitterViewData
{
Content = entry.Element(xmlns + "content").Value,
Updated = entry.Element(xmlns + "updated").Value,
AuthorName = entry.Element(xmlns + "author").Element(xmlns + "name").Value,
AuthorUri = entry.Element(xmlns + "author").Element(xmlns + "uri").Value,
Link = (from o in entry.Descendants(xmlns + "link")
where o.Attribute("rel").Value == "image"
select new { Val = o.Attribute("href").Value }).First().Val
}).ToList();
}
}
}
return View(twitterViewData);
}
VB.NET
Public Function Index() As ActionResult
Dim request = TryCast(WebRequest.Create("http://search.twitter.com/search.atom?q=worldcup&rpp=5"), HttpWebRequest)
Dim twitterViewData = New List(Of TwitterViewData)()
If request IsNot Nothing Then
Using response = TryCast(request.GetResponse(), HttpWebResponse)
Using reader = New StreamReader(response.GetResponseStream())
Dim document = XDocument.Parse(reader.ReadToEnd())
Dim xmlns As XNamespace = "http://www.w3.org/2005/Atom"
twitterViewData = (
From entry In document.Descendants(xmlns + "entry")
Select New TwitterViewData With {.Content = entry.Element(xmlns + "content").Value, .Updated = entry.Element(xmlns + "updated").Value, .AuthorName = entry.Element(xmlns + "author").Element(xmlns + "name").Value, .AuthorUri = entry.Element(xmlns + "author").Element(xmlns + "uri").Value, .Link = (
From o In entry.Descendants(xmlns + "link")
Where o.Attribute("rel").Value = "image"
Select New With {Key .Val = o.Attribute("href").Value}).First().Val}).ToList()
End Using
End Using
End If
Return View(twitterViewData)
End Function
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!