Read Value for media:thumbnail Linq to Xml

28. June 2010
I was playing around with my HULU rss feed the other day and was using silverlight to just read and display the queue. I was using Linq to XML to read the feed. I know in Silverlight 4 there is a class to help read the RSS Feed, but I wanted to make my own class and use Linq to XML just in case I wanted to expand on it.

Anyhow, the reason for this post is I couldn't figure out how to parse the following elements in the item element

After searching around a bit I found some good examples, but they were either outdated or just didn't seem to work the way I wanted. However, they did get me going in the right direction.

In short here, here is one way of accomplishing it

Here is part of the code in how it's used:

Part of RSS Feed

Here is the LINQ to XML code to read the RSS Feed.

Here is a link to the GetNamespaceOfPrefix method()

http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.getnamespaceofprefix.aspx

Ok...enjoy.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C#

Twitter API Friends Timeline and LINQ to XML

15. April 2009

So here is some code to grab the friends timeline utilizing LINQ to XML.  There are two parts to this. 1) a function to make the request to their API and bring back the response and 2) use LINQ to XML to read the friends timeline.

You'll notice in my code I add the querystring item "count" because it's open to bring back the items you want. Without "count" it brings back the 20 items.

1) Code to make the request and return a string (which is the XML) brought back

public string GetFriendsTimeline(int count)
{
        WebRequest req = WebRequest.Create(string.Format("http://twitter.com/statuses/friends_timeline.xml?count={0}", count));
        NetworkCredential creds = new NetworkCredential(_username, _password);

        req.Credentials = creds;
        req.Method = "GET";
        WebResponse resp = (WebResponse)req.GetResponse();

        Stream respStream = resp.GetResponseStream();
        StreamReader reader = new StreamReader(respStream);

        string respString = reader.ReadToEnd();

        reader.Close();
        respStream.Close();
        resp.Close();

        return respString;
}

_username and _password are private variables stored in the class that holds this function so replace the variable with your credentials if need be during testing

2) The function that utilizez LINQ to XML to easily roll through the XML string in the Friends Timline

private void GetRecentTweets()
{

        timelineString = objTwitter.GetFriendsTimeline(10);

        XDocument xDoc = XDocument.Parse(timelineString);

        var resultTweets = from tweets in xDoc.Descendants("status")
                          select new
                          {
                              ID = tweets.Element("id").Value,
                              Text = tweets.Element("text").Value,
                              Source = tweets.Element("source").Value,
                              UserID = tweets.Element("user").Element("id").Value,
                              UserName = tweets.Element("user").Element("name").Value,
                              UserScreenName = tweets.Element("user").Element("screen_name").Value,
                              UserImageUrl = tweets.Element("user").Element("profile_image_url").Value,
                              CreatedDate = tweets.Element("created_at").Value,
                          };

 //...the rest of your code here for

}

remember the function in step one is located in a class so a simple copy and paste of this code will not work in your program.  However, the two functions combined will deciding how you want them to be laid out in your program.  Happy programming.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C# ,

Export Event Logs Windows 2003 in .NET - C#

7. April 2009

This is a small console application I wrote to export the event log files in C#.  I did this because in Windows 2003 I couldn't find a way to get it to XML by doing a Save As or anything like that.  I did notice in Windows Vista that you can save it as XML...which is nice, but couldn't find a way for Server 2003.  Anyway I did this more or less because I wanted to see stuff happening with ASP.NET 2.0 since it writes to the Application Log with any unhandled exception.  So as I always do I'm just going to paste the entire code...about 140 lines. This program takes the following information:

Server Name
Source of Entry (i.e., ASP.NET 2.0.50727.0)
Log Type ( i.e., Application, Security)

This ends up exporting the event log from Windows 2003 to an XML file.

Oh yea, as always use it at your own risk, if anything it just won't work because it can't open the Event Log or can't write the file...not too much error catching going on in here since I wrote it in about 30 minutes or so.

using System;
using System.Diagnostics;
using System.IO;

namespace AppLogExporter
{
    class Program
    {

        static int evtType = 0;
        static int logType = 0;
        static string serverName = "";
        static string eventSource = "";

        static void Main(string[] args)
        {
            ShowEntryMenu();
            GetLogInfo();
            Console.ReadLine();
        }

        #region "Show Entry Menu"
        private static void ShowEntryMenu()
        {

            Console.Write("Enter Server Name: ");
            serverName = Console.ReadLine();

            Console.WriteLine();

            Console.Write("Enter Source for Log Entry: ");
            eventSource = Console.ReadLine();

            Console.WriteLine();

            Console.WriteLine("Select Log Type");
            Console.WriteLine("1:Application");
            Console.WriteLine("2:Security");
            Console.WriteLine("3:Setup");
            Console.Write("Log Type: ");

            logType = Int32.Parse(Console.ReadLine());

            Console.WriteLine();

            Console.WriteLine("Select the Event Type to Export");
            Console.WriteLine("1:Error");
            Console.WriteLine("2:Information");
            Console.WriteLine("3:Warning");
            Console.WriteLine("4:All");
            Console.Write("Event Type: ");

            evtType = Int32.Parse(Console.ReadLine());

        }

        #endregion

        #region "GetLogInfo"
        private static void GetLogInfo()
        {
            EventLog eventLog = null;
            string eventType = string.Empty;
            StreamWriter swLog = new StreamWriter("D:\\ApplicationLogExport.xml", false);
           
            swLog.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            swLog.WriteLine("<SystemLog>");

            try
            {
                switch (logType)
                {
                    case 1: eventLog = new EventLog("Application", serverName);
                        break;
                    case 2: eventLog = new EventLog("Security", serverName);
                        break;
                    case 3: eventLog = new EventLog("Setup", serverName);
                        break;
                    default: break;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: Unable to open log file for the following reason: {0}", ex.Message);
                return;              
            }
          
            switch (evtType)
            {
                case 1: eventType = "Error";
                    break;
                case 2: eventType = "Information";
                    break;
                case 3: eventType = "Warning";
                    break;
                case 4: eventType = "All";
                    break;
                default: break;

            }

            //READ EACH ENTRY IN THE LOG AND WRITE TO FILE
            foreach (EventLogEntry evtLogEntry in eventLog.Entries)
            {

                if (eventType == "All")
                {
                    //get all event types
                    if (evtLogEntry.Source == eventSource)
                    {
                        Console.WriteLine(string.Format("Entry: {0} - Message: {1}", evtLogEntry.EntryType, evtLogEntry.Message));
                        swLog.WriteLine("<Entry>");
                        swLog.WriteLine("<Type>{0}</Type>", evtLogEntry.EntryType);
                        swLog.WriteLine("<EntryDate>{0}</EntryDate>", evtLogEntry.TimeWritten);
                        swLog.WriteLine("<Message>{0}</Message>", evtLogEntry.Message);
                        swLog.WriteLine("</Entry>");
                    }
                }
                else
                {
                    if (evtLogEntry.Source == eventSource && evtLogEntry.EntryType.ToString() == eventType)
                    {
                        Console.WriteLine(string.Format("Entry: {0} - Message: {1}", evtLogEntry.EntryType, evtLogEntry.Message));

                        swLog.WriteLine("<Entry>");
                        swLog.WriteLine("<Type>{0}</Type>", evtLogEntry.EntryType);
                        swLog.WriteLine("<EntryDate>{0}</EntryDate>", evtLogEntry.TimeWritten);
                        swLog.WriteLine("<Message><![CDATA[{0}]]></Message>", evtLogEntry.Message);
                        swLog.WriteLine("</Entry>");
                    }
                }
            }

            swLog.WriteLine("</SystemLog>");
            swLog.Close();
            swLog.Dispose();
        }
        #endregion
    }
}

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C#