LINQ to XML subquery

I didn’t find a clear example online about how to do a subquery of an element, so I thought I would share.

First, here’s the XML I’m reading:


<response> 
    <name>Test</name> 
    <states> 
        <state>OH</state> 
        <state>MI</state> 
    </states> 
</response> 

I want to use LINQ to parse through the responses and create new objects for each one of them.  The object I created to hold the info is below:


 [Serializable()]
    public class User
    {
        public string userName { get; set; }a
        public List states { get; set; }

        public User(string UserName,List States)
        {
            userName = UserName;
            states = States;
        }
    }

I was at a loss at how to get the state elements into my “States” list. Here’s the query I ended up using:


var responses = from response in loginResult.Descendants("response")
      select new
      {
            authenticationToken = response.Element("authentication_token").Value,
            states = (from st in response.Descendants("states").Elements()
                         select st.Value).ToList(),

      };

It was obvious after I got it and I probably should have been able to figure it out quicker, but such as life when learning something new.

Sharepoint

I’ve been meaning to post my thoughts about my recent foray into Sharepoint for awhile now and since I finally have some time, here goes.

Development on Sharepoint sucks if you’re not doing it directly on the server.
-I can’t even count the number of hours I’ve spent copying a .cab file to the server, running stsadm.exe -o addwppack and restarting IIS.
Documentation for development on Sharepoint sucks.
-This was the first time I’ve ever developed against a platform with such limited documentation/examples. While it was pretty interesting, I would prefer to not do something like this again.

That being said, I thought I would post some of the code that I’ve written that may be useful. This first webpart allows you to search for users in the AD tree associated with your site:

        
private DataTable Search()
{
    SPSite siteCollRoot = new SPSite(SPContext.Current.Site.Url);
    DataTable dt = new DataTable();

    //must log in to the SSP and give the NT_AuthorityAuthenticated
    //users the Manage User Profiles and Personal site permissions
    //here (http://localhostssp/admin/_layouts/ManageServicePermissions.aspx)
    //in order for the code below to work
    //for all users
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {

        using (SPSite siteColl = new SPSite(siteCollRoot.ID))
        {
            ServerContext context = ServerContext.GetContext(siteColl);
            UserProfileManager upm = new UserProfileManager(context);
            UserProfile currentProfile;
            IEnumerator enumProfs = upm.GetEnumerator();
            bool continueEnum = true;

            DataColumn dc = new DataColumn("Title");
            dt.Columns.Add(dc);

            dc = new DataColumn("EMail");
            dt.Columns.Add(dc);

            dc = new DataColumn("JobTitle");
            dt.Columns.Add(dc);

            dc = new DataColumn("Department");
            dt.Columns.Add(dc);

            dc = new DataColumn("WorkPhone");
            dt.Columns.Add(dc);

            dc = new DataColumn("ID");
            dt.Columns.Add(dc);

            dc = new DataColumn("LastName");
            dt.Columns.Add(dc);

            dc = new DataColumn("FirstName");
            dt.Columns.Add(dc);

            while (continueEnum)
            {
                try
                {
                    continueEnum = enumProfs.MoveNext();
                }

                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION in enum: " + e.ToString());
                    continueEnum = enumProfs.MoveNext();
                }
                currentProfile = (UserProfile)enumProfs.Current;

                try
                {
                    DataRow dr = dt.NewRow();
                    dr["ID"] = currentProfile.ID.ToString();
                    dr["Title"] = currentProfile["PreferredName"].ToString();
                    dr["EMail"] = currentProfile["WorkEmail"].ToString();
                    dr["JobTitle"] = currentProfile["Title"].ToString();
                    dr["Department"] = currentProfile["Department"].ToString();
                    dr["WorkPhone"] = currentProfile["WorkPhone"].ToString();
                    dr["LastName"] = currentProfile["LastName"].ToString();
                    dr["FirstName"] = currentProfile["FirstName"].ToString();

                    dt.Rows.Add(dr);
                }
                catch (Exception ex)
                {

                }

            }

        }
    });

    return dt;
}

Newport Aquarium

On a whim, the wife and I decided to check out the Newport Aquarium yesterday. We were already at the Levee (enjoying Dim Sum at Pacific Moon) and really didn’t have anything else going on, so we headed over. I was a little shocked at the ticket price ($18.95) but after spending an hour there, we decided that it was worth it. They have some really cool exhibits like a “live flight” bird sanctuary and the requisite shark tank. Definitely check it out if you’ve got a free afternoon.

Database design – Primary Keys

This is an interesting article on the selection of a primary key during database design: http://rapidapplicationdevelopment.blogspot.com/2007/08/in-case-youre-new-to-series-ive.html.

I’ve always wondered what the best primary key was and while there are pros and cons for both natural and surrogate keys, it seems that surrogate keys are the way to go.

First Week

Well, I’ve completed my first full week at my new job and everything has been going well so far. Even after only one week, it’s obvious that there are some very smart people here and I look forward to working with them in the future.

I’ve also started to work on my first two projects. The first is a simple addition to a web application, but it looks really cool so far. The second is constructing an intranet using Sharepoint 2007. I had experience with Sharepoint before, but there are so many things that I’ve had to learn about it for this project. I was a little uneasy at first, but I think that I’ll be able to get it done.

Jump to Ubuntu

I finally did it. After years of dual-booting various flavors of Linux, I’ve made the leap to using Ubuntu Linux as the primary OS on my home computer.

I’ve been saying for years that Linux’s biggest flaw is it’s dreadful configuration tools and lack of documentation. I can remember spending a whole week trying to get my wireless card set up in Mandrake only to give up and remove it from my system (and I’m an IT guy!).  Previous version of Ubuntu weren’t much better, either.  While they provided more configuration options, it still wasn’t as easy as setting up a Windows installation.

However, in my opinion, Ubuntu 7.10 is the closest any version of Linux has ever come to being an actual Windows desktop replacement.  I can actually configure and use it without pulling my hair out.  I even got the super cool Compiz Fusion effects to work without too much trouble.

There are a few things that still need to be ironed out (I still had to manually install ndiswrapper and install wireless drivers from a CD) but overall, I believe that Linux on the desktop is finally starting to become a reality.

New job

After 3 and half years as a consultant, I’ve accepted an offer to work for Barefoot, a local advertising agency. I will be the sole .Net developer in the interactive design department, so wish me luck. The other guys there seem to like Ruby on Rails, but that’s not my problem. 😉

I’ll see if I can show them the merits of .Net and bring them over to the dark side.

Prototypical First Blog Post

Hello all. This is your typical “first post” post. I hope to use this blog to convey my experiences in both my work and my life.

Switch to our mobile site