Tuesday, February 24, 2009

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

if you got this error dealing with XML


{"Namespace Manager or XsltContext needed. This query has a prefix, variable, or
user-defined function."}


You need to make sure that you include the XmlNamespaceManager

//Setting up NSManager
XmlNamespaceManager man = new XmlNamespaceManager(rssDoc.NameTable);
man.AddNamespace("msncp", "urn:schemas-microsoft-com/contentpublishing/content");

take a look at the example here
http://carso-owen.blogspot.com/2009/02/how-to-get-data-from-msn-navigation.html

string manipulation websites you must know as web designer or perhaps blogger

Some times some web editor doesn't allow you to post certain characters. Here u can hack it by encode the html
http://www.string-functions.com/htmlencode.aspx

You got a string put you want it a one line sentance, here is the best website to remote the breaks
http://www.textfixer.com/tools/remove-line-breaks.php

You u want to generate dummy text here is the
Lorem ipsum site
http://www.lipsum.com/

How to get data from msn navigation feed rss using .net

I have a project to develop a website for MSN and I have to read the feed (Microsoft own schema). Here is the code to save ur time :)



Let say i want to get a feed from this link

http://sg.msn.com/rss/navfeedrssensg.aspx



By using few lines of codes to get the feed:

MSNNavFeedMenu feedmenu = new
MSNNavFeedMenu("http://sg.msn.com/rss/navfeedrssensg.aspx");
string feedLinkMenu =
feedmenu.CreateNavLinks(feedmenu.GetNavLinks(4), 5);
string feedToolLinkMenu =
feedmenu.CreateNavToolLinks(feedmenu.GetNavToolLinks(), 5);


Here is the class:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Web.Security;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;


public class NavFeedRss
{

private string _Title;
private string _Link;
private string _Icon;
private string _Description;

public string Description
{
get
{
return _Description;
}
}

public string Title
{
get
{
return _Title;
}
}

public string Link
{
get
{
return _Link;
}
}

public string Icon
{
get
{
return _Icon;
}
}


public NavFeedRss(string title, string description, string link, string icon)
{
_Title = title;
_Link = link;
_Icon = icon;
_Title = title;
_Description = description;
}

}

/// <summary>
/// Summary description for MSNNavFeedMenu
/// </summary>
public class MSNNavFeedMenu
{
private string rssUrl;


public MSNNavFeedMenu(string rsslink)
{
rssUrl = rsslink;
}


public string CreateNavLinks(List<NavFeedRss> RssNavMenu, int columns)
{
string itemTitles = String.Empty;

if (RssNavMenu.Count > 0)
{
//Create list


//Create dynamic row with specified fixed column
int row = Convert.ToInt32(Math.Floor(Convert.ToDouble(RssNavMenu.Count / columns)));
int reminder = RssNavMenu.Count % columns;

int c = 0;
int temp = 0;
for (int r = 1; r <= row; r++)
{
itemTitles += "<ul class='linklist1'>";
for (c = temp; c < (columns * r); c++)
{
if (r == 1)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'>" + RssNavMenu[c].Title + "</a>";
itemTitles += "</li>";

}
temp = columns * r;
itemTitles += "</ul>";
}


//Add reminder item
itemTitles += "<ul class='linklist1'>";

for (c = temp; c < temp + reminder; c++)
{
if (c == temp)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'>" + RssNavMenu[c].Title + "</a>";
itemTitles += "</li>";
}
itemTitles += "</ul>";


//Closing list


}



return itemTitles;
}


public string CreateNavToolLinks(List<NavFeedRss> RssNavMenu, int columns)
{

string itemTitles = String.Empty;

if (RssNavMenu.Count > 0)
{

//Create dynamic row with specified fixed column
int row = Convert.ToInt32(Math.Floor(Convert.ToDouble(RssNavMenu.Count / columns)));
int reminder = RssNavMenu.Count % columns;

int c = 0;
int temp = 0;
for (int r = 1; r <= row; r++)
{
itemTitles += "<ul class='linkedimglinklist1 cf'>";
for (c = temp; c < (columns * r); c++)
{
if (r == 1)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}



itemTitles += "<a href='" + RssNavMenu[c].Link + "'><img src='" + RssNavMenu[c].Icon + "' width='25' height='20' alt='Windows Live Messenger' /><span><strong>" + RssNavMenu[c].Title + "</strong></span></a>";
itemTitles += "</li>";

}
temp = columns * r;
itemTitles += "</ul>";
}


//Add reminder item
itemTitles += "<ul class='linkedimglinklist1 cf'>";

for (c = temp; c < temp + reminder; c++)
{
if (c == temp)
{
itemTitles += "<li class='first'>";
}
else
{
itemTitles += "<li>";
}

itemTitles += "<a href='" + RssNavMenu[c].Link + "'><img src='" + RssNavMenu[c].Icon + "' width='25' height='20' alt='Windows Live Messenger' /><span><strong>" + RssNavMenu[c].Title + "</strong></span></a>";

itemTitles += "</li>";
}
itemTitles += "</ul>";


//Closing list


}



return itemTitles;
}

public List<NavFeedRss> GetNavToolLinks()
{

List<NavFeedRss> rsstitles = new List<NavFeedRss>();

WebRequest myRequest = WebRequest.Create(rssUrl);
WebResponse myResponse = myRequest.GetResponse();

Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);



//Setting up NSManager
XmlNamespaceManager man = new XmlNamespaceManager(rssDoc.NameTable);
man.AddNamespace("msncp", "urn:schemas-microsoft-com/contentpublishing/content");


XmlNodeList rssItemsImg = rssDoc.SelectNodes("rss/channel/msncp:gtl/msncp:networklists/msncp:toollist/msncp:tool/msncp:imagelink/msncp:image", man);
XmlNodeList rssItemsLnk = rssDoc.SelectNodes("rss/channel/msncp:gtl/msncp:networklists/msncp:toollist/msncp:tool/msncp:imagelink/msncp:link", man);


string title = "";
string link = "";
string description = "";
string enclosure = "";




for (int i = 0; i < rssItemsLnk.Count; i++)
{
XmlNode rssDetail;


rssDetail = rssItemsImg.Item(i).SelectSingleNode("msncp:src", man);
if (rssDetail != null)
{
enclosure = rssDetail.InnerText;
}
else
{
enclosure = "";
}



rssDetail = rssItemsImg.Item(i).SelectSingleNode("msncp:alternatetext", man);
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}



rssDetail = rssItemsLnk.Item(i).SelectSingleNode("msncp:text", man);
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}


rssDetail = rssItemsLnk.Item(i).SelectSingleNode("msncp:url", man);
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}

rsstitles.Add(new NavFeedRss(title, description, link, enclosure));

}





return rsstitles;
}


public List<NavFeedRss> GetNavLinks(int start)
{

List<NavFeedRss> rsstitles = new List<NavFeedRss>();

WebRequest myRequest = WebRequest.Create(rssUrl);
WebResponse myResponse = myRequest.GetResponse();

Stream rssStream = myResponse.GetResponseStream();
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

string title = "";
string link = "";
string description = "";
string enclosure = "";

for (int i = start; i < rssItems.Count; i++)
{
XmlNode rssDetail;

rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("enclosure");
if (rssDetail != null)
{
enclosure = rssDetail.Attributes["url"].InnerText;
}
else
{
enclosure = "";
}


rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}

rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}


rsstitles.Add(new NavFeedRss(title, description, link, enclosure));

}

return rsstitles;
}

}

Sunday, February 22, 2009

using store procedure is not my style in .net

During the past I am quite excited about using store procedure and I used it quite often for some performance reason and right now I found out that performance gain is a big trade off of the code maintainability and flexibility in my system. The reason I say so is because when i want to perform a select statement for instance, I need to create a new store procedure and over the time I have a hard time to maintain it. Actually, putting SQL statements inside application code is far more better than having storing store procedure over the server. Just think about by concatenate sql in a string with some condition and forget about store procedure which create so much complication. What do you think? Is life more simpler?

Friday, February 20, 2009

jquery get and set value dealing with asp.net control

this demo show that how I get value from the lblSearchType (Label) and populate data into txtKeyword (TextBox) asp.net control when I click different radio button using jquery

$('#<%=rdKeywordSearch.ClientID%>').click(function() {
$('#<%=lblSearchType.ClientID%>').text("Keyword");
$('#<%=txtKeyword.ClientID%>_input').attr("value", "Keyword");
$('#<%=txtKeyword.ClientID%>_input').attr("value", "Enter keyword here...");
});

$('#<%=rdLocationSearch.ClientID%>').click(function() {
$('#<%=lblSearchType.ClientID%>').text("Location");
$('#<%=txtKeyword.ClientID%>_input').attr("value", "Location");
$('#<%=txtKeyword.ClientID%>_input').attr("value", "Enter location here...");
});

Wednesday, February 18, 2009

Resize picture function in C#


using System.Drawing;
using System.Drawing.Drawing2D;

//w means the output size of the image width


public static bool ResizePhoto(string src, string dest, int w)
{
System.Drawing.Image imgTmp = default(System.Drawing.Image);
double sf = 0;
System.Drawing.Bitmap imgFoto = default(System.Drawing.Bitmap);
imgTmp = System.Drawing.Image.FromFile(src);
if ((imgTmp.Width > w))
{
sf = imgTmp.Width / w;
imgFoto = new System.Drawing.Bitmap(w, (int)(imgTmp.Height / sf));
Rectangle recDest = new Rectangle(0, 0, w, imgFoto.Height);
Graphics gphCrop = Graphics.FromImage(imgFoto);
gphCrop.SmoothingMode = SmoothingMode.HighQuality;
gphCrop.CompositingQuality = CompositingQuality.HighQuality;
gphCrop.InterpolationMode = InterpolationMode.High;
gphCrop.DrawImage(imgTmp, recDest, 0, 0, imgTmp.Width, imgTmp.Height, GraphicsUnit.Pixel);
}
else
{
imgFoto = (Bitmap)imgTmp;
}
System.Drawing.Imaging.Encoder myEncoder = default(System.Drawing.Imaging.Encoder);
System.Drawing.Imaging.EncoderParameter myEncoderParameter = default(System.Drawing.Imaging.EncoderParameter);
System.Drawing.Imaging.EncoderParameters myEncoderParameters = default(System.Drawing.Imaging.EncoderParameters);
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
System.Drawing.Imaging.ImageCodecInfo jpegICI = null;
int x = 0;
for (x = 0; x <= arrayICI.Length - 1; x++)
{
if ((arrayICI[x].FormatDescription.Equals("JPEG")))
{
jpegICI = arrayICI[x];
break;
}
}
myEncoder = System.Drawing.Imaging.Encoder.Quality;
myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);
myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, 60L);
myEncoderParameters.Param[0] = myEncoderParameter;
try
{
imgFoto.Save(dest, jpegICI, myEncoderParameters);
}
catch (Exception ex)
{
throw ex;
}
imgFoto.Dispose();
imgTmp.Dispose();
return true;
}

Tuesday, February 17, 2009

ajax json jquery call with passing parameters using ASP.Net

If you want to load a content faster than before, you need to use ajax to load content. To get started you need to:

HTML

<div id="EventContent" class="loading"/>

SCRIPT


$.ajax({
type: "POST",
url: "RSSReader.asmx/GetRSSReader",
data:
"{controlName:'events.ascx'}",
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function(data)
{
$('#EventContent').removeClass('loading').html(data);
}
});



APP_CODE

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public
class RSSReader : System.Web.Services.WebService {
[WebMethod]
public
string GetRSSReader(string controlName)
{
Page page = new
Page();
UserControl ctl =
(UserControl)page.LoadControl("~/controls/" +
controlName);
page.Controls.Add(ctl);
StringWriter writer = new
StringWriter();
HttpContext.Current.Server.Execute(page, writer,
false);
return writer.ToString();
}

}



Notice that the bold text is the parameter name and it have to be same. It is recommended to be string