Now that Silverlight 2.0 was released I have been really started to play with it. I was hesitant to play around with it/get serious about it until the final release came due to my experience with ASP.NET AJAX (formerly ATLAS). When ATLAS was CTP and Beta, it was quite a moving target. This time I let the dust settle before playing with it. I did do a little bit of stuff recently with Silverlight 1.0, mostly just hooked it up on my site, didn’t actually develop any Silverlight based products.
This time around, that will change as I am really impressed with how easy it is to get a functional product with it in just a little bit of time. Like most things, dedicate some time to it and you’ll pick it up fast.
I started Tuesday morning with Scott Guthrie’s series of posts on how to get started and quickly was able to get through the process of building this simple project. Since Tuesday I have been taking what I have learned and trying to build a few things with it, that I will share soon.
One of the great things about this technology is the server side aspects of this tool. My weakness has always been user interfaces and client side scripting. I typically spend more time in the middle tier writing business logic, and/or ASP.NET Server controls and developer tools. Frankly, I HATE writing Javascript by hand and having to deal with the intricacies of each and every browser. I like server side programming where I can write code in C#.
As I side effect of learning Silverlight 2.0, I was also exposed to LINQ to XML. Of course I read about it, but since I don’t necessarily spend a lot of time querying and/or generating XML, I really haven’t spent much time or needed to spend much time learning about it. I was more excited about LINQ to Objects, LINQ to SQL, etc. But after doing Scott’s walkthrough and in particular this post, I love LINQ to XML.
This is such a powerful and *simple* way to generate XML and parse XML. Tonight I was working on generating sitemap file that Google could use to index a site I am working on, and essentially, I wanted to loop through a product catalog and build a <url> node for each product returned in the data set.
See the code below for an example.
1: var allProducts = (from products in DataContext.Products
2: let ProductSubCategoryID = DataContext.ProductSubCategory_Product_XREFs.Where(p => p.ProductID == products.ProductID).Take(1).SingleOrDefault().ProductSubCategoryID
3: let ProductCategoryID = DataContext.ProductCategory_ProductSubCategory_XREFs.Where(p => p.ProductSubCategoryID == ProductSubCategoryID).Take(1).SingleOrDefault().ProductCategoryID
4: let LinkPath = "http://www.msrfx.com/ProductDetails.aspx?ProductID=" + products.ProductID + "&ProductCategoryID=" + ProductCategoryID + "&ProductSubCategoryID=" + ProductSubCategoryID
5: select new
6: {
7: products.ProductID,
8: products.Voided,
9: LinkPath,
10: ProductCategoryID,
11: ProductSubCategoryID
12: }).Where(p => p.ProductCategoryID != null && p.ProductSubCategoryID != null && p.Voided == false);
13:
14: XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
15: XDocument doc = new XDocument(
16: new XDeclaration("1.0", "UTF-8", "yes"),
17: new XElement(ns + "urlset",
18: from p in allProducts select new XElement(ns + "url",
19: new XElement(ns + "loc", p.LinkPath),
20: new XElement(ns + "lastmod", string.Format("{0:yyyy-MM-dd}", DateTime.Now)),
21: new XElement(ns + "changefreq", "monthly"),
22: new XElement(ns + "priority", "0.9")
23: )
24: )
25: );
This was a way cleaner way to generate the XML document I needed than using the “old” System.Xml namespace.
These tools just keep getting better and better!