Friday, December 30, 2011

html encoding table

if you need to search for html encoding table you can go to this link below:

http://www.zytrax.com/tech/web/entities.html


or

http://www.w3schools.com/TAGS/ref_entities.asp


mostly when you googling in the net, what you will find is URL encoding instead of HTML Encoding..

*julie*

Thursday, December 29, 2011

create time duration using c#

Here are the code I created for that purpose:

private string calculateLastUpdate(DateTime dt1, DateTime dt2)
{
string strTimeValue = "";
string timespans;
string[] arrTimespan;
TimeSpan ts = dt1.Subtract(dt2);
timespans = ts.ToString().Replace(":", ".");

if (timespans.Substring(0, 8) == "00.00.00")
timespans = timespans.Substring(9, timespans.Length - 10);
else if (timespans.Substring(0, 5) == "00.00")
timespans = timespans.Substring(6, timespans.Length - 10);
else if (timespans.Substring(0, 2) == "00")
timespans = timespans.Substring(3, timespans.Length - 10);

arrTimespan = timespans.Split('.');

strTimeValue = getTimeUnit(arrTimespan.Count(), Convert.ToInt32(arrTimespan[0]));
return strTimeValue;
}

private string getTimeUnit(int position, int value)
{
string result = "";
switch (position)
{
case 5: result= "day";
if (value / 365 >= 1)
{
result = "year";
value = value / 365;
}
else if (value / 30 >= 1)
{
result = "month";
value = value / 30;
}
else if (value / 7 >= 1)
{
result = "week";
value = value / 7;
}
break;
case 4: result = "hour"; break;
case 3: result = "minute"; break;
case 2: result = "second"; break;
case 1: result = "milisecond"; break;
}


if (value > 1)
result += "s";
result = Convert.ToString(value) + " " + result + " ago";

return result;
}


hope its usefull :)

Wednesday, December 28, 2011

set global variable in javascript

source: http://snook.ca/archives/javascript/global_variable

set the global var using this:

window.myVariable = 1;

instead of
var myVariable = 1;

because sometimes its not working :P

Thursday, December 22, 2011

Open new window using a href

here is the sample code:

<a href="javascript: void(0)"
onclick='javascript:window.open("http://www.google.com");return false;'>New Window</a>

btw if you want to encode your html, can go to this site:


Thursday, December 15, 2011

connect to web service with SLL settings

I got a task to explore an API to get some data to be displayed in a new interface. The problem is the web service is using SSL. Actually the first thing in my mind is using web reference from VS.net, so when I tried to add web reference, there is an error show up like this:

unable to download wsdl file

Tried many times but this error still occurs, then I search in google, and last thing I found is this site: http://debugguru.blogspot.com/2008/06/unable-to-download-wsdl-file-while.html
and indeed it solved the problem, wkwkwk.. Just close the solutions, and reopen, I tried to add web reference again, it works, lol.

But apparently I don't use the web reference. I try to connect to wsdl using Service Reference.
I try to connect, but this error occurs:

Could not establish trust relationship for the SSL/TLS secure channel with authority 'xx.xx.x.xx'.

I search over the net then I found this code:

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

public class TestUtils

{

public static void OverrideCertificateValidation()

{

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertValidate);

}

private static bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)

{

return true;

}

}


Hope it will be useful for you :)

-julie-

working with xml

Few days ago, I got a task to provide a Chart with with the chart highlights which is a brief description of the chart value. To get the chart is easier then to get the values of the chart. '
There are 3 links to get the values from. From the first xml file, I manage to find a way to get the value based on the node name.

Get nodes (and values) after selected node

Dictionary dict = new Dictionary();

private
void getElement(string URLString, string tagName)

{

XmlTextReader reader = new XmlTextReader(URLString);

reader.WhitespaceHandling = WhitespaceHandling.None;

XmlDocument doc = new XmlDocument();

doc.Load(reader);

if ((doc != null))

{

try

{

int i = 0;

XmlNodeList elemList = doc.GetElementsByTagName(tagName);

foreach (XmlNode elem in elemList)

{

for (int j = 0; j < elem.ChildNodes.Count; j++)

{

callChildNode(elem.ChildNodes[j]);

}

i++;

}

}

catch (Exception ex)

{

// exception

throw ex;

}

}

}

private void callChildNode(XmlNode node)

{

if (node.Name != "#text")

if (node.ChildNodes.Count == 1 )

dict.Add(node.Name, node.InnerText);

for (int j = 0; j < node.ChildNodes.Count; j++)

{

callChildNode(node.ChildNodes[j]);

}

}


From the line above, all the nodes with value after the tag that we defined will be listed inside the dictionary. That is for the case if you have a different xml tag that you wish to get the node value from (even if the node inside is the same with the node from the other tag).

------

Get node value based on another node's value

After the first XML, I thought everything is solved, but then when I saw the second XML, it was totally different from the first xml. Dang!! so I have to find a way to get value of a node based on another node's value.

I searched in the internet and got a sample of code from the link below:

http://stackoverflow.com/questions/6203666/xpath-select-a-node-based-on-another-node

Below are the code

var doc = new XmlDocument();
doc.
LoadXml(xmlString);
XmlElement element = (XmlElement)doc.SelectSingleNode("Items/Item[Code='MyCode']/Value");
Console.WriteLine(element.InnerText);


Hope this two code will be useful..