You also have the option to replace the traditional Task Manager by the Process Explorer software menu: Options-> Replace Task Manager
Yosi Havia 's c# web development blog. הבלוג של יוסי חביה נושאי תכנות שונים
public class CacheProvider
{
public T GetData<T, P>(string cacheKey,
Func<P, T> getfromDbFunc, object param) where T : class
{
T item = HttpRuntime.Cache.Get(cacheKey) as T;
P p = (P)param;
if (item == null)
{
item = getfromDbFunc(p);
HttpContext.Current.Cache.Insert(cacheKey, item, null,
DateTime.UtcNow.AddMinutes(10),
System.Web.Caching.Cache.NoSlidingExpiration);
}
return item;
}
}
I'll be glad to get suggestions or improvements
protected void Page_Load(object sender, EventArgs e)
{
CacheProvider cacheProvider = new CacheProvider();
int userid = 17;
var user = cacheProvider.GetData<string, int>("user_" + userid, getUser, userid);
}
public string getUser(int id)
{
//TODO: retrieve user from db
return "newuser";
}

create TABLE [dbo].[Persons](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Counter] [int] NULL,
[BirthDate] [datetime] NULL,
CONSTRAINT [PK_Persons] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
) ON [PRIMARY]
GO
<%
foreach (string columnName in columns)
{
columnAlias = DnpUtils.SetCamelCase(DnpUtils.TrimSpaces(table.Columns[columnName].Alias));
columnLanguageType = table.Columns[columnName].LanguageType;
%>
private <%=columnLanguageType%> _<%=columnAlias%>;
<%
}
%>


HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();
html.LoadHtml(
@"<html>
<head></head>
<body>
<div id='content'>
<a target='_blank' href='http://yosi-havia.blogspot.com' class='FirstName'>Yosi</a>
<a href='http://yosi-havia.blogspot.com' class='LastName'>Havia</a>
</div>
</body>
</html>");
HtmlNode document = html.DocumentNode;
//@@@ get all elements with content id in html(result: 1 element)
IEnumerable<HtmlNode> list1 = document.QuerySelectorAll("#content");
List<HtmlNode> lst1 = list1.ToList<HtmlNode>();
//@@@ get all elements with FirstName class name in html(result: 1 element)
IEnumerable<HtmlNode> list2 = document.QuerySelectorAll(".FirstName");
List<HtmlNode> lst2 = list2.ToList<HtmlNode>();
//@@@ get all anchor tags in html(result: 2 elements)
IEnumerable<HtmlNode> list3 = document.QuerySelectorAll("a");
List<HtmlNode> lst3 = list3.ToList<HtmlNode>();
//@@@ get all elements in html(result: 6 elements)
IEnumerable<HtmlNode> list4 = document.QuerySelectorAll("*");
List<HtmlNode> lst4 = list4.ToList<HtmlNode>();
//@@@ get element with attribue name 'target' that starts with '_bl' in html(result: 1 element)
IEnumerable<HtmlNode> list5 = document.QuerySelectorAll("a[target^='_bl']");
List<HtmlNode> lst5 = list5.ToList<HtmlNode>();
public partial class _Default : System.Web.UI.Page
{
public string UserNameKey;
public string PasswordKey;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserNameKey"] = UserNameKey = Guid.NewGuid().ToString();
Session["PasswordKey"] = PasswordKey = Guid.NewGuid().ToString();
}
}
protected void lnkSend_Click(object sender, EventArgs e)
{
if (Session["UserNameKey"] != null
&& Session["PasswordKey"] != null)
{
string UserNameValue = Request[Session["UserNameKey"].ToString()];
string PasswordValue = Request[Session["PasswordKey"].ToString()];
}
}
}
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
User name: <input type="text" name="<%=UserNameKey%>" />
<br />
Password: <input type="password" name="<%=PasswordKey%>" />
<br />
<asp:LinkButton ID="lnkSend" runat="server"
onclick="lnkSend_Click" >Send</asp:LinkButton>
</form>
</body>
</html>
<hPerson>
<Header>
<FirstName>Yosi</FirstName>
<LastName>Havia</LastName>
<Age>30</Age>
<Education>BA</Education>
</Header>
<hChildren>
<hChild>
<hName>Carol</hName>
<hAge>13</hAge>
</hChild>
<hChild>
<hName>Angela</hName>
<hAge>15</hAge>
</hChild>
<hChild>
<hName>Benjamin</hName>
<hAge>17</hAge>
</hChild>
</hChildren>
</hPerson>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" omit-xml- declaration="yes"/>
<xsl:param name="Gender"></xsl:param>
<xsl:template match="/">
<Person>
<Gender>
<xsl:choose>
<xsl:when test="$Gender='1'">
Male
</xsl:when>
<xsl:otherwise>
Female
</xsl:otherwise>
</xsl:choose>
</Gender>
<xsl:copy-of select="/hPerson/Header"/>
<Data>
<Children>
<xsl:for-each select="/hPerson/hChildren/hChild">
<xsl:sort select="hName" />
<Child>
<FirstName>
<xsl:value-of select="hName"/>
</FirstName>
<Age>
<xsl:value-of select="hAge"/>
</Age>
</Child>
</xsl:for-each>
</Children>
</Data>
</Person>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Gender>
Male
</Gender>
<Header>
<FirstName>Yosi</FirstName>
<LastName>Havia</LastName>
<Age>30</Age>
<Education>BA</Education>
</Header>
<Data>
<Children>
<Child>
<FirstName>Angela</FirstName>
<Age>15</Age>
</Child>
<Child>
<FirstName>Benjamin</FirstName>
<Age>17</Age>
</Child>
<Child>
<FirstName>Carol</FirstName>
<Age>13</Age>
</Child>
</Children>
</Data>
</Person>
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace XSLT
{
class XsltObj
{
protected static XslCompiledTransform _oXslTransform;
protected static object _oSyncXsl = new object();
public string FormatXmlWrapper(string sEncoding, XsltArgumentList
oXsltArgumentList,XmlDocument oOriginXmlDoc, string sXslPath)
{
string sRetVal = null;
if (_oXslTransform == null)
{
LoadXslWrapper(sXslPath, ref _oXslTransform, ref _oSyncXsl);
}
//@@@ make the transform
try
{
sRetVal = TransformData(sEncoding, _oXslTransform, oOriginXmlDoc,
oXsltArgumentList);
}
catch (Exception ex)
{
throw ex;
}
return sRetVal;
}
protected void LoadXslWrapper(string sXslPath,
ref XslCompiledTransform oXslTransform, ref object oSyncXsl)
{
lock (oSyncXsl)
{
if (oXslTransform == null)
{
XslCompiledTransform oTempXslTransform =
new XslCompiledTransform();
//@@@ get the xml path from configuration
LoadXsl(sXslPath, ref oTempXslTransform);
oXslTransform = oTempXslTransform;
}
}
}
protected void LoadXsl(string sXslPath, ref XslCompiledTransform
oXslCompiledTransform)
{
//@@@ load the xsl document(only once)
XmlDocument xslDocument = new XmlDocument();
XmlUrlResolver urlResolver = new XmlUrlResolver();
urlResolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
xslDocument.XmlResolver = urlResolver;
try
{
//@@@ load the xsl document
xslDocument.Load(sXslPath);
XPathNavigator oXPathNavigator = xslDocument.CreateNavigator();
oXslCompiledTransform.Load(oXPathNavigator,
XsltSettings.TrustedXslt, urlResolver);
}
catch (Exception ex)
{
throw ex;
}
}
protected string TransformData(string sEncoding, XslCompiledTransform
oXslTransform, XmlDocument xmlDocument, XsltArgumentList argumentList)
{
StringBuilder sb = new StringBuilder();
string sDeclaration;
//@@@ decide the xml declaration up to the Encoding
if (string.IsNullOrEmpty(sEncoding))
sDeclaration = "version=\"1.0\"";
else
sDeclaration = "version=\"1.0\" encoding=\"" + sEncoding + "\"";
//@@@ make the xsl Transform
using (XmlWriter output = XmlWriter.Create(sb))
{
output.WriteProcessingInstruction("xml", sDeclaration);
XPathNavigator oXPathNavigator = xmlDocument.CreateNavigator();
oXslTransform.Transform(oXPathNavigator, argumentList, output);
return sb.ToString();
}
}
}
}
static void Main(string[] args)
{
XsltObj oXsltObj = new XsltObj();
XsltArgumentList oXsltArgumentList = GetXsltArguments();
XmlDocument oXmlDocument = new XmlDocument();
string sXmlPath = @"Person.xml";
string sXslPath = @"Person.xslt";
oXmlDocument.Load(sXmlPath);
string sXml = oXsltObj.FormatXmlWrapper("UTF-8", oXsltArgumentList,
oXmlDocument, sXslPath);
}
protected static XsltArgumentList GetXsltArguments()
{
//@@@ create the arguments for the xsl
XsltArgumentList list = new XsltArgumentList();
list.AddParam("Gender", string.Empty, "1");
return list;
}
private void forceDownloadRemoteFile(string sFileVirtualPath)
{
string sFileName = System.IO.Path.GetFileName(sFileVirtualPath);
WebClient oWebClient = new WebClient();
MemoryStream oMemoryStream;
try
{
byte[] bytes = oWebClient.DownloadData(sFileVirtualPath);
oMemoryStream = new MemoryStream(bytes);
}
finally
{
oWebClient.Dispose();
}
BinaryReader oBinaryReader = new BinaryReader(oMemoryStream);
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}", sFileName));
Response.BinaryWrite(oBinaryReader.ReadBytes((int)oMemoryStream.Length));
oBinaryReader.Close();
Response.Flush();
Response.End();
}
protected void Page_Load(object sender, EventArgs e)
{
string sFileVirtualPath = @"http://localhost/TryWebSite/TextFile.txt";
forceDownloadRemoteFile(sFileVirtualPath);
}
public class RootData
{
public HeaderElement Header;
public ListSubjectsList;
}
public class HeaderElement
{
public int DataType;
}
public class Subject
{
public string SubjectName;
public int SubjectID;
public int SubjectType;
}
<?xml version="1.0" encoding="utf-8"?>
<RootData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Header>
<DataType>1</DataType>
</Header>
<SubjectsList>
<Subject>
<SubjectName>Name1</SubjectName>
<SubjectID>1</SubjectID>
<SubjectType>1</SubjectType>
</Subject>
<Subject>
<SubjectName>Name2</SubjectName>
<SubjectID>2</SubjectID>
<SubjectType>2</SubjectType>
</Subject>
<Subject>
<SubjectName>Name3</SubjectName>
<SubjectID>3</SubjectID>
<SubjectType>3</SubjectType>
</Subject>
</SubjectsList>
</RootData>
public string SerializeObject(Type oType, object oRootData)
{
//@@@ Create xml from the object
MemoryStream oMemoryStream = null;
XmlTextWriter oXmlTextWriter = null;
string sXml = null;
try
{
oMemoryStream = new MemoryStream();
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
oXmlTextWriter = new XmlTextWriter(oMemoryStream, Encoding.UTF8);
oXmlSerializer.Serialize(oXmlTextWriter, oRootData);
oMemoryStream = (MemoryStream)oXmlTextWriter.BaseStream;
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
sXml = oUTF8Encoding.GetString(oMemoryStream.ToArray());
}
catch (Exception)
{
throw;
}
finally
{
if (oMemoryStream != null)
oMemoryStream.Close();
if (oXmlTextWriter != null)
oXmlTextWriter.Close();
}
return sXml;
}
public object DeserializeObject(Type oType, string sXml)
{
//@@@ Create object from the xml
MemoryStream oMemoryStream = null;
object oRootData = null;
try
{
XmlSerializer oXmlSerializer = new XmlSerializer(oType);
UTF8Encoding oUTF8Encoding = new UTF8Encoding();
Byte[] Bytes = oUTF8Encoding.GetBytes(sXml);
oMemoryStream = new MemoryStream(Bytes);
oRootData = oXmlSerializer.Deserialize(oMemoryStream);
}
catch (Exception)
{
throw;
}
finally
{
if (oMemoryStream != null)
oMemoryStream.Close();
}
return oRootData;
}
public RootData CreateData()
{
RootData oRootData = new RootData();
HeaderElement oHeaderElement = new HeaderElement();
oHeaderElement.DataType = 1;
oRootData.Header = oHeaderElement;
Subject oSubject1 = new Subject();
oSubject1.SubjectID = 1;
oSubject1.SubjectName = "Name1";
oSubject1.SubjectType = 1;
Subject oSubject2 = new Subject();
oSubject2.SubjectID = 2;
oSubject2.SubjectName = "Name2";
oSubject2.SubjectType = 2;
Subject oSubject3 = new Subject();
oSubject3.SubjectID = 3;
oSubject3.SubjectName = "Name3";
oSubject3.SubjectType = 3;
ListSubjects = new List ();
Subjects.Add(oSubject1);
Subjects.Add(oSubject2);
Subjects.Add(oSubject3);
oRootData.SubjectsList = Subjects;
return oRootData;
}
RootData oRootData = CreateData();
string sXml = SerializeObject(typeof(RootData), oRootData);
oRootData = (RootData)DeserializeObject(typeof(RootData), sXml);
public uint IPAddressToLongBackwards(string IPAddr)
{
System.Net.IPAddress oIP = System.Net.IPAddress.Parse(IPAddr);
byte[] byteIP = oIP.GetAddressBytes();
uint ip = (uint)byteIP[0] << 24;
ip += (uint)byteIP[1] << 16;
ip += (uint)byteIP[2] << 8;
ip += (uint)byteIP[3];
return ip;
}
SELECT countryCode2
from [ip-to-country]
where ipFrom <= @ip
and ipTo >= @ip
//@@@ I have Dictionary with Key_Event5 object as the key
Dictionary<Key_Event5, Event5_Employment> dictEmployment;
//@@@ 4 support ContainsKey function compare by value, i'll implement Key_Event5 on this way:
public class Key_Event5
{
public int iFormID { get; set; }
public int iEmploymentCode { get; set; }
public Key_Event5(int iFormID, int iEmploymentCode)
{
this.iFormID = iFormID;
this.iEmploymentCode = iEmploymentCode;
}
public override bool Equals(object obj)
{
Key_Event5 oKey_Event5 = obj as Key_Event5;
if (oKey_Event5 == null)
return false;
//@@@ Compare by iFormID and iEmploymentCode
return Equals(iFormID, oKey_Event5.iFormID)
&& Equals(iEmploymentCode, oKey_Event5.iEmploymentCode);
}
public override int GetHashCode()
{
return iFormID.GetHashCode() ^ iEmploymentCode.GetHashCode();
}
}
//@@@ And now this line will work
dictEmployment.ContainsKey(oKey_Event5)
<?xml version='1.0'?>
<Persons>
<Person Height="180">
<FullName>Yosi Havia</FullName>
<Age>18</Age>
</Person>
<Person Height="177">
<FullName>Yosi Cohen</FullName>
<Age>22</Age>
</Person>
<Person Height="169">
<FullName>Itay Cohen</FullName>
<Age>32</Age>
</Person>
</Persons>
//@@@ Select all the persons in 18 age
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[Age = '" + 18 + "']");
//@@@ Select all the persons with age greater than 17
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[Age > '" + 17 + "']");
//@@@ Select all the persons that contains Yosi in their names
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person/FullName[contains(.,'" + "Yosi" + "')]");
//@@@ Select all the persons with 180 height(attribute)
XmlNodeList oXmlNodeList =
XmlPersons.SelectNodes("/Persons/Person[@Height = '" + "180" + "']");
<?xml version='1.0'?>
<Persons>
<Person Height="180">
<FullName>Yosi Havia</FullName>
<Age>18</Age>
</Person>
<Person Height="177">
<FullName>Yosi Cohen</FullName>
<Age>22</Age>
</Person>
<Person Height="169">
<FullName>Itay Cohen</FullName>
<Age>32</Age>
</Person>
</Persons>
<?xml version='1.0'?>
<Persons>
<Person Height="175">
<FullName>Yosi Havia</FullName>
<Age>15</Age>
</Person>
</Persons>
XmlDocument XmlPersons1 = new XmlDocument();
XmlPersons1.Load(@"Persons.xml");
XmlDocument XmlPersons2 = new XmlDocument();
XmlPersons2.Load(@"Persons2.xml");
XmlNode oXmlNewData = XmlPersons2.SelectSingleNode("/Persons/Person[FullName = '" + "Yosi Havia" + "']");
XmlNode targetNode = XmlPersons1.SelectSingleNode("/Persons/Person[FullName = '" + "Yosi Havia" + "']");
//@@@ Add the node from XmlPersons2.xml to XmlPersons.xml
XmlNode sourceNode = XmlPersons1.ImportNode(oXmlNewData, true);
//@@@ Replace the original node
XmlPersons1.DocumentElement.ReplaceChild(sourceNode, targetNode);
<?xml version="1.0"?>
<Persons>
<Person Height="175">
<FullName>Yosi Havia</FullName>
<Age>15</Age>
</Person>
<Person Height="177">
<FullName>Yosi Cohen</FullName>
<Age>22</Age>
</Person>
<Person Height="169">
<FullName>Itay Cohen</FullName>
<Age>32</Age>
</Person>
</Persons>
Stopwatch oStopwatch = new Stopwatch();
oStopwatch.Start();
//@@@ Start of code to make the performence check
Thread.Sleep(3000);
//@@@ End of code to make the performence check
oStopwatch.Stop();
TimeSpan oTimeSpan = oStopwatch.Elapsed;
Console.WriteLine(oTimeSpan.ToString());
00:00:02.9994520