Archive for the ‘Windows’ Category

Linq to XML

23 April 2010 Leave a comment

Here is a very short post giving an example on how to use Linq to XML. 

Let’s say we have the following XML

<?xml version="1.0" encoding="utf-8" ?>
    <project title="Kraankuil">
        <technologies>Gimp, ASP.NET, CMS</technologies>
        <launched>12 March 2010</launched>
        <description>Website for a small family owned B&amp;B in South Africa.</description>

We will do a class to easily handle the data like this

public class Project
    public string Title { get; set; }
    public string Screenshot { get; set; }
    public string Uri { get; set; }
    public string Technologies { get; set; }
    public string Launched { get; set; }
    public string Description { get; set; }

I will be using this is a web service where I use this code

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Project> ProjectList()
    XDocument dataDoc;

    // load the file - from cache if possible
    if (HttpContext.Current.Cache["dataDoc"] == null)
        dataDoc = XDocument.Load(Server.MapPath("~/App_Data/Data.xml"));
        HttpContext.Current.Cache["dataDoc"] = dataDoc; // store it in the cache
    } else {
        dataDoc = (XDocument)HttpContext.Current.Cache["dataDoc"];

    //get the projects from the file
    var projectList = from project in dataDoc.Descendants("project")
                      select new Project
                          Title = project.Attribute("title").Value,
                          Screenshot = project.Element("screenshot").Value,
                          Technologies = project.Element("technologies").Value,
                          Launched = project.Element("launched").Value,
                          Description = project.Element("description").Value,
                          Uri = project.Element("uri").Value

    return projectList.ToList();

Note: This is NOT a best practices guide so do use common sense, it is only meant to get you started

MVC Buddy Class

26 January 2010 5 comments

My latest “toy” is the new .NET 4.0 and MVC framework which has a few really nice additions.  In this post I will briefly cover one of them, Buddy Classes.  Although there are disagreements as to whether or not this is a good or bad thing, I like the fact that it makes life a bit simpler on smaller projects.

First off I created a VERY simple database with one table.

CREATE TABLE [dbo].[Friend](
	[UserId] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [nvarchar](100) NOT NULL,
	[LastName] [nvarchar](100) NOT NULL,
	[BirthDate] [date] NOT NULL,
	[UserId] ASC


This table will just hold a list of people.  The BrithDate column is not really for this post but for a later one where I will demonstrate how to use user controls for editing specific data types.

Then I added my ADO.NET Entity Data Set, in my Model folder, which created an object for this table.  As we all know, if I change this class the changes will be replace every time it is re-generated.  This is a problem when you want to add some attributes to the class.  What the guys at Microsoft has done is given us a way (hacky some say) to get passed this problem.   With this new feature we can simply create a Buddy Class (normal class in the Model folder) and decorate it to look like this:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace MVCBuddyClass.Models
    public partial class Friend

    public class FriendMeta
        [DisplayName("First Name")]
        public string FirstName { get; set; }
        [DisplayName("Last Name")]
        public string LastName { get; set; }
        [DisplayName("Date of Birth")]
        public DateTime BrithDate { get; set; }

In this code you will notice the public partial class Friend, which has the same name as the partial class generated by the Entity Framework.  My own Friend class in this case gets the attribute [MetadataType(typeof(FriendMeta))] which you need using System.ComponentModel.DataAnnotations; for.  This hooks it up to my “buddy” class, FriendMeta.
FriendMeta contains all the properties with their attributes that we want to set, like DisplayName("First Name")] in this case.  There are many other attributes that you can set which more of will be covered in later posts, this one was simply to display the basic idea of a buddy class.

This should only be used for classes that get auto-generated to keep DRY.