I’m back!
Posted by Martin in ASP.NET/C#, Web development on 2010-10-22
It’s been almost a year since I wrote on my blog post. That’s way too long! It’ll defiantly not take that long until next time, I promise!!
Here is a little tip on how you can extend .NET built-in objects. With something called Extension Methods you can add a method to e.g. the String object (just like you can in JavaScript). Below is a little extension method that I find useful. From time to time you want to repeat a string X amount of times. With this String.Repeat() extension you can do that like so:
Example
string reallyAngry = "Argh ".Repeat(3); // reallyAngry is now: "Argh Argh Argh ".
StringExtensions.cs
using System;
using System.Text;
public static class StringExtensions
{
public static string Repeat(this string self, int count)
{
if (count <= 0)
{
return String.Empty;
}
else
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++)
{
builder.Append(self);
}
return builder.ToString();
}
}
}
Repeat
Transforming XML/XSLT using .NET 3.5
Posted by Martin in ASP.NET/C#, Web development, XML/XSLT on 2009-12-14
Here is a simple example of how you can apply a XSLT stylesheet to a XML file and deliver the result to the browser using ASP.NET 3.5.
default.aspx.cs
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class _default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
// Source paths
string xmlFilePath = Server.MapPath("hello-world.xml");
string xsltFilePath = Server.MapPath("hello-world.xslt");
// Load XML
XPathNavigator nav = new XPathDocument(new XmlTextReader(xmlFilePath)).CreateNavigator();
// Stream for the transformed XML
MemoryStream stream = new MemoryStream();
// Load XSLT & transform
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(xsltFilePath);
transformer.Transform(nav, null, stream);
// Write output
using (StreamReader reader = new StreamReader(stream)) {
stream.Position = 0;
Response.Write(reader.ReadToEnd());
reader.Close();
}
}
}
Yes, that’s it! Here below are the rest of the code used to complete a fully working demo.
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
hello-world.xml
<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl" href="hello-world.xslt"?> <text>Hello World!</text>
hello-world.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><xsl:value-of select="text"/></title>
</head>
<body>
<h1><xsl:value-of select="text"/></h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Tip! To have the HTML doctype declared in your XSLT document you need to wrap it in a <xsl:text> tag with the disable-output-escaping attribute set to yes. And don’t forget to swap the leading and tailing < and > for < and >.
Output
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
In Uganda
It’s been a while since I wrote a post. That’s due to 1; the lack of internet access and 2; I don’t want to spend time indoors in front of a computer when I can explore Africa or drink beer in the sun.
Anyways, now we are at a internet cafe in Uganda’s capital, Kampala. We arrived in Uganda Thursday the 12th after a super nice stay in South Africa. We rented a car in Cape Town and traveled along the famous Garden Route towards our final destination, Port Elizabeth. Along the way we stayed in Struis Bay where we made a visit to the southern most point of Africa. In Plettenberg Bay (aka “Plett”), a super nice little town where we surfed, spotted whales, went hiking and chilled out with big cats like cheetahs and leopards. Unfortunately we didn’t (bungy) jump of any bridge as planned. It was very expensive, plus we had been up really early surfing for hours, so we didn’t really feel like it. Our final stop was Port Elizabeth before leaving South Africa for Uganda.
In Uganda I have been to Kampala and the town where Maria lives, Masaka. It’s a small, nice town in the countryside. Maria and here buddy’s live in a big house on the top of a hill with a great view over the valley where the town is located. The house is located on a big nice lot with lots of greens and wildlife in and around.
Uganda is a very poor country and the feeling is a little bit like being at a big music festival like the one in Roskilde. It’s dirty, people everywhere just walking/hanging around, small shed shops and fire smoke in the air (not as many drunk people though). The car pollution, the roads and the traffic is very bad! But it’s a nice very green country.
Tomorrow we will be off on our whitewater rafting adventure. After that it’s safari and then Zanzibar. I’m really locking forward to it!
I will probably not be able to upload any photos until I find a Wi-Fi (which is pretty rare) or I get home :(
Comments
The commenting function on my blog has been out of order, but is now back on track. Feel free to leave a comment when ever you’d like!
Cape Point and Boulders
Sunday
So, after meeting Maria and our driver at the airport we went to the Hotel, Chartfield Guesthouse, in Kalk Bay just south of Cape Town. I was starving after all the waiting at the airport, so we decided to go and get some food right away. We found a small café just around the corner where we got a salad and just talked. After all, we had 10 weeks of catching up to do.
Later we went back to the hotel to get a shower and to unpack our bags. The room we have is just perfect. It’s fairly large with a king size bed, large bathroom and a super nice view.
After a shower and some rest, we went out for some nice dinner and wine down by the harbor.
Monday
After a good nights sleep, we got up and got a really nice breakfast. Pancakes, omelette, bread, fruit, freshly made orange juice and coffee. Sweet!
Our rental car got delivered at 9:30 as planned. We packed our bags, bought sandwiches and got on our way towards Cape Point. At first I was a bit scared of driving on “the wrong side” of the road, but after a while I felt like I had it under control. After all, it’s been 10 years since I got my South African driver’s license.
Cape Point/Cape of Good Hope was even more beautiful than I remembered it to be. It’s probably the most beautiful place I’ve ever seen. The weather was nice and sunny, but very windy! Some times it even hard to stand straight. But that didn’t spoil the great view. In the park we also saw lots of birds, turtles, lizards and baboons.
On the way back to the hotel we made the mandatory stop at Boulders beach where you can watch African penguins. They are about 50 cm tall and really cute. Especially when they walk – they do that wiggling walk penguins do.
The day ended with a nice dinner and wine at FSH.
Here below are some photos from today (Tuesday, but I’ll write about that later)
In Cape Town at last
Finally back in Cape Town! It took me 10 years to do it. When I left CT back in ’99 I had set my goals to be back within 5 years (that’s when my South Africa drivers licence would expire). But that doesn’t matter now. The importent thing is that I’m here!
I left Malmö C yesterday at 14:02 and arrived at Heathrow at 18. Had some dinner at Wagamama and bordered the 747 to Cape Town. After some 5 beers, a second air-plane-dinner, the movie Moon and an episode of The new old Christine, I was ready to get some sleep. Unfortunately for me, some people had gathered by the toilets and were having loud discussions. But finally I managed to get three hours of sleep or so.
Waking up I had a cloud-free view over Namibia, beautiful. After landing, waiting for 500 people to get off (I was in the far back), migration, baggage claim and customs I entered South Africa again!
So now here I am, dead tired, waiting for my love to show her pretty face. I can’t wait!! I guess I’ll be crashing once at our hotel.
That’s it for now. Now I’m going to try to see if I can take a nap here, in the pretty noise domestic arrivals hall, at Cape Town International Airport.
Going to Africa
Next Saturday I’ll be off to Africa to visit my girl, Maria. First stop will be Cape Town followed by Port Elizabeth, Masaka, Kampala, Serengeti, Dar es-Salaam and Zanzibar. At least that’s the plan so far. The adventures side of the trip will be covered by safari and whitewater rafting. And maybe, just maybe, we will be jumping of the 216 meter high Bloukrans bridge – the world’s highest bungyjump. Whoo-hoooo!!!
I will keep you all updated from here as often as I can. I don’t know how often I will be able to go online, but I’ll do my best. I guess it will be alright in South Africa, but I’m not too sure about Uganda and Tanzania.
I’ve also opened up a Flickr account where I’m hoping to publish some photos along the way. Again, it all depends on the ability to go online.
That’s it for now. Later!
iPhone Scroll To bookmarklet
Posted by Martin in Browsers, JavaScript, Web development on 2009-10-12
Do you also get tiered of flickering your finger to scroll up and down loooong web pages? I was, so I wrote this JavaScript bookmarklet to do it for me. It enables you to enter a %-value where to (vertically) scroll on the page.
How to use it:
Just add and save a bookmark on any page. Open it for edit. Name it “Scroll to …” (or what ever) and paiste the JavaScript code below into the URL field.
javascript:window.scroll(window.pageXOffset,(prompt(%22Scroll%20to%20...%20(0-100)%22)/100)*document.body.clientHeight);
To use it, just select the bookmark from the bookmarks menu, enter a value between 0-100 (where 0 is the page top and 100 is the page bottom) into the prompt and hit OK.
Happy scrolling!
Output the source XML with XSLT
Posted by Martin in Tip, Web development on 2009-10-09
Today I just learned something I’ve been looking for for a while. It’s super simple, but it can be very helpful. For those of us that needs to write and edit XSLT for XML you don’t own and can’t view – in my case from SharePoint. Here is a single line of XSLT that will output the source XML to your page.
<xsl:copy-of select="."/>
That’s it! I told you, super simple.
Remember that you will probably not see the code on the page since the nodes probably won’t be recognized by the browser.


