Archive for November, 2009
Using System.Uri’s Segments property and List().ForEach
Posted by fr3dr1k | Filed under C#
You know the feeling when you see someone use a property and you go, nice, I never knew that. Well last night that happened to me after reading Scott Hanselman’s article on Windows Powershell. In the article he created a script that automatically downloaded his podcasts. To do this he use the System.Uri class which has a property called Segments. Segments returns a string array which consists of elements in a Uri separated by a forward slash ‘/’. So lets say you have this url:
http://developer.yahoo.com/yap/guide/caja-support.html
You could then use System.Uri to get all the bits in the Uri like this:
Uri url = new Uri("http://developer.yahoo.com/yap/guide/caja-support.html");
string[] arrUri = url.Segments;
var item = from u in arrUri
select u;
foreach (string s in item)
{
Console.WriteLine(s);
}
Something else I started doing or using yesterday is the ToList().ForEach delegate method. I noticed it in the LinqToTwitter api:
var twitterTrends = from trends in tCtx.Trends
select trends;
twitterTrends.ToList().ForEach(t =>
Console.WriteLine(t.Query));
The ForEach works on a list as well, so if you take the same code I wrote earlier using a foreach loop for the Uri segments you can rewrite that as:
item.ToList().ForEach(s => Console.WriteLine(s));
The variable s is an anonymous type so it infers from the type what type it is. That is shorter code, not sure if its more efficient, but it sure looks nicer.
Inkscape supports XAML
Posted by fr3dr1k | Filed under WPF
I have been using Inkscape now for a while and I was surprised to see XAML as an export format. This means that I can design UI elements in Inkscape and re-use them in Visual Studio 2008 for WPF/Silverlight applications. This means that I do not have to buy Expression Design, which is a bargain.
Will email truly die?
Posted by fr3dr1k | Filed under General
I got a Google Wave invite earlier this week and I must admit I have not logged in again since and honestly I have not felt compelled to, but that may just be me. Maybe it is just me, maybe I am not as forward and revolutionary thinking as the people over at Google. To be completely honest though I do find it quite scary when Google start tapping into our everyday lives, even if they profess to be squeaky clean and all. Just because they seem to be a lesser evil doesnt mean they are less evil. Back to the topic at hand though, will email truly die? In South Africa I do not really think it would, maybe in years to come. What would replace it though? Mobile messaging? I mean the South African environment is filled with Mxiteers and who is to say that in 10 or so years from now Mxit would replace email as contemporary communication tool. But as things stand at the moment you cannot dispose of email really. I mean some people live from Outlook, their whole job and existence is documented there, what about them? Email remains a very useful tool to communicate and to keep a track of communication, although I do believe this is where Google Groups is cool. As I said at the start maybe I don’t get Google Wave yet. Until then I guess email will remain.
Getting POST values with an ASHX file
Posted by fr3dr1k | Filed under AJAX, C#, Web Development, Web Technologies
Today I had this scenario where I wanted to post items from multiple HTML input elements to a generic handler (.ashx) file without using the action attribute of the form. Specifying the action meant that that you are navigated away from the page where the action is happening, which means re-creating UI logic. How did I achieve this? By using an XMLHttpRequest and using a POST method. GET places everything inside a querystring, which is ok, but I just wondered what would happen if the content was too long for the querystring. I guess the same can be said for POST, but it just seems POST uses a different way to transfer the data. So lets say you had this JavaScript:
getXMLHTTPPostObject: function(url, elementName, parameters) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById(elementName).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", url, true);
xmlhttp.send(parameters);
}
I use this function to create an XMLHttpRequest object by passing it:
- the URL for the AJAX call
- an elementName to put the result of request in
- A parameter list
I then have a function like this:
addPost: function() {
objXMLHTTP.getXMLHTTPPostObject("url to handler", "categoryTemp", "postTitle=" + document.getElementById("txtPostTitle").value + "&blogpost=" + document.getElementById("txtBlogPost").value);
}
The function gets the values of two HTML input elements and passes it as parameters. The parameters are then used in the POST HTTPMethod. My next challenge was to get the data in the generic handler (.ashx) so that I can process it. I also wanted to return the data from the ASHX file to see that its processed successfully. So in my handler I created this code:
context.Response.ContentType = "text/plain";
System.IO.Stream body = context.Request.InputStream;
System.Text.Encoding encoding = context.Request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
//if (context.Request.ContentType != null)
//{
// context.Response.Write("Client data content type " + context.Request.ContentType);
//}
string s = reader.ReadToEnd();
string[] content = s.Split('&');
for (int i = 0; i < content.Length; i++)
{
string[] fields = content[i].Split('=');
context.Response.Write("
" + fields[0] + "
");
context.Response.Write("
" + fields[1] + "
");
}
//context.Response.Write(s);
body.Close();
reader.Close();
I first create a class of type Stream that is instantiated through the Response.InputStream property, after which I set the content encoding for the response object. I then create a StreamReader instance and call its ReadToEnd method. After this I do some string manipulation and return the text back to the XMLHttpRequest object, which then writes the content to an HTML Element.
Getting a single XAttribute or XElement
Posted by fr3dr1k | Filed under C#, LINQ to XML
I have come across two ways querying XElements or XAttributes:
- Make an object IEnumerable and loop through the results
- Use the Single() method to return a single object only
Typically you may have a situation where you write some Linq-to-XML like this:
IEnumerableattrib = from att in elemInner.Attributes() where (string)att.Name.ToString() == "sectionName" select att;
To loop through the results you have to use a foreach loop like this:
foreach (XAttribute innerAttrib in attrib)
{
builder.AppendLine("
" + innerAttrib.Value +"
");
}
If however you just wanted a single attribute result, you could write the same code like this:
XAttribute attribDisplayStyle = (from att in elemInner.Attributes()
where (string)att.Name.ToString() == "sectionName"
select att).Single();
Then you only have a single XAttribute instance and you dont need a foreach loop:
attribDisplayStyle.Value;
You would apply the same logic to XElement as well.
