14 03 2012

Get Random Elements from a List



public static List getRandomItemsFromList(int returnCount, List list)
        {   // Mucahid USLU, 14.03.2012
            List returnList = new List();
            T rndItem;
           
            int randomInt = 0 ;
            int i=0;
            while (i < returnCount)
            {
                randomInt = new Random().Next(list.Count);
                rndItem = list[randomInt];
                returnList.Add(rndItem);
                list.Remove(rndItem);
                i++;
            }

            return returnList;
        }

08 03 2012

Add Disk Clean to Windows Server 2008 R2

> Control Panel
> .. Add Feature
> .. Desktop Experience
> Then restart server.


 

03 02 2012

Simple C# Atom and Rss feed parser

.NET already has quite a few open source RSS and ATOM libraries for parsing feeds.
Update
.NET 4 now has built in RSS support in the framework in the System.ServiceModel.Syndication namespace.
A lot of the time these APIs are a bit overkill and bulky for what you need - the Link, Title, Content and Publish date. With LINQ-to-XML this is easily achievable with the the XDocument object and some basic node searching.
The only drawback with this method is it doesn't remove the namespaces from the nodes, which is why there are Where lookups going on instead of straight XName comparisons. When you have a namespace declaration in your XML document at the root, all XElement nodes than get called "{namespace}node". XElement has an overriden comparison operator to compare with a string, but unfortunately it uses this full name instead of its LocalName property, which would just be "Node".
I haven't done any performance comparisons but I will make a wild guess that it's faster than most feed APIs for parsing, as it ignores all the unwanted nodes. For downloading the feed versus a custom HttpWebRequest it may be slower as it uses the XDocument.Load method.

Usage

FeedParser parser = new FeedParser();
var items = parser.Parse("http://www.footballtoday.org.uk/rss/home/",FeedType.RSS);

Source

Firstly the Item class for holding a Feed's metadata. You might want to add a Site property to this for grouping per site.
///
/// Represents a feed item.
///
public class Item
{
public string Link { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PublishDate { get; set; }
public FeedType FeedType { get; set; }
 
public Item()
{
Link = "";
Title = "";
Content = "";
PublishDate = DateTime.Today;
FeedType = FeedType.RSS;
}
}
Any bad publish dates are turned into DateTime.Min. The idea behind the virtual methods is that you can subclass and do something more. At present though these methods don't return an Item but the entire list so there isn't much gained from overriding them. Sometime in the future I will probably add it.
///
/// A simple RSS, RDF and ATOM feed parser.
///
public class FeedParser
{
///
/// Parses the given and returns a .
///
///
public IList Parse(string url, FeedType feedType)
{
switch (feedType)
{
case FeedType.RSS:
return ParseRss(url);
case FeedType.RDF:
return ParseRdf(url);
case FeedType.Atom:
return ParseAtom(url);
default:
throw new NotSupportedException(string.Format("{0} is not supported",feedType.ToString()));
}
}
 
///
/// Parses an Atom feed and returns a .
///
public virtual IList ParseAtom(string url)
{
try
{
XDocument doc = XDocument.Load(url);
 
// Feed/Entry
var entries = from item in doc.Root.Elements().Where(i => i.Name.LocalName == "entry")
select new Item
{
FeedType = FeedType.Atom,
Content = item.Elements().First(i => i.Name.LocalName == "content").Value,
Link = item.Elements().First(i => i.Name.LocalName == "link").Attribute("href").Value,
PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "published").Value),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value
};
 
return entries.ToList();
}
catch
{
return new List();
}
}
 
///
/// Parses an RSS feed and returns a .
///
public virtual IList ParseRss(string url)
{
try
{
XDocument doc = XDocument.Load(url);
 
// RSS/Channel/item
var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
select new Item
{
FeedType = FeedType.RSS,
Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value
};
 
return entries.ToList();
}
catch
{
return new List();
}
}
 
///
/// Parses an RDF feed and returns a .
///
public virtual IList ParseRdf(string url)
{
try
{
XDocument doc = XDocument.Load(url);
 
// is under the root
var entries = from item in doc.Root.Descendants().Where(i => i.Name.LocalName == "item")
select new Item
{
FeedType = FeedType.RDF,
Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "date").Value),
Title = item.Elements().First(i => i.Name.LocalName == "title").Value
};
 
return entries.ToList();
}
catch
{
return new List();
}
}
 
private DateTime ParseDate(string date)
{
DateTime result;
if (DateTime.TryParse(date, out result))
return result;
else
return DateTime.MinValue;
}
}
 
///
/// Represents the XML format of a feed.
///
public enum FeedType
{
///
/// Really Simple Syndication format.
///
RSS,
///
/// RDF site summary format.
///
RDF,
///
/// Atom Syndication format.
///
Atom
}

27 01 2012

IEnumerable vs IQueryable

The difference is that IQueryable is the interface that allows LINQ-to-SQL (LINQ.-to-anything really) to work. So if you further refine your query on an IQueryable, that query will be executed in the database, if possible.
For the IEnumerable case, it will be LINQ-to-object, meaning that all objects matching the original query will have to be loaded into memory from the database.
In code:
IQueryable<Customer> custs = ...;
// ..
var customers = custs.Where(c => c.IsGold);
That code will execute SQL to only select gold customers. The following code, on the other hand, will execute the original query in the database, then filtering out the non-gold customers in the memory:
IEnumerable<Customer> custs = ...;
// ..
var customers = custs.Where(c => c.IsGold);
This is quite an important difference, and working on IQueryable can in many cases save you from returning too many rows from the database. Another prime example is doing paging: If you use Take and Skip on IQueryable, you will only get the number of rows requested; doing that on an IEnumerable will cause all of your rows to be loaded in memory.

25 01 2012

Linq to DataTable

public static DataTable LINQToDataTable(IEnumerable varlist)
        {   // Mucahid USLU mucuslu@hotmail.com

             DataTable dtReturn = new DataTable();

             // column names
             PropertyInfo[] oProps = null;

             if (varlist == null) return dtReturn;

             foreach (T rec in varlist)
             {
                 // Use reflection to get property names, to create table, Only first time, others will follow
                  if (oProps == null)
                  {
                       oProps = ((Type)rec.GetType()).GetProperties();
                       foreach (PropertyInfo pi in oProps)
                       {
                            Type colType = pi.PropertyType;

                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()     
                            ==typeof(Nullable<>)))
                             {
                                 colType = colType.GetGenericArguments()[0];
                             }

                            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                       }
                  }

                  DataRow dr = dtReturn.NewRow();

                  foreach (PropertyInfo pi in oProps)
                  {
                       dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue
                       (rec,null);
                  }

                  dtReturn.Rows.Add(dr);
             }
             return dtReturn;
        }

01 12 2011

HTML ASCII Characters

HTML ASCII Characters

codes

characters

character names

ASCII: space (usually one em wide)
! ! ASCII: exclamation mark
" " ASCII: quotation mark
# # ASCII: number sign
$ $ ASCII: dollar sign
% % ASCII: percent sign
& & ASCII: ampersand
' ' ASCII: apostrophe-quote
( ( ASCII: opening parenthesis
) ) ASCII: closing parenthesis
* * ASCII: asterisk
+ + ASCII: plus sign
, , ASCII: comma
- - ASCII: hyphen-minus
. . ASCII: period
/ / ASCII: slash
0 0 ASCII: digit zero
1 1 ASCII: digit one
2 2 ASCII: digit two
3 3 ASCII: digit three
4 4 ASCII: digit four
5 5 ASCII: digit five
6 6 ASCII: digit six
7 7 ASCII: digit seven
8 8 ASCII: digit eight
9 9 ASCII: digit nine
: : ASCII: colon
; ; ASCII: semicolon
< < ASCII: less-than sign
= = ASCII: equals sign
> > ASCII: greater-than sign
? ? ASCII: question mark
@ @ ASCII: commercial at
A A ASCII: Latin Capital Letter A
B B ASCII: Latin Capital Letter B
C C ASCII: Latin Capital Letter C
D D ASCII: Latin Capital Letter D
E E ASCII: Latin Capital Letter E
F F ASCII: Latin Capital Letter F
G G ASCII: Latin Capital Letter G
H H ASCII: Latin Capital Letter H
I I ASCII: Latin Capital Letter I
J J ASCII: Latin Capital Letter J
K K ASCII: Latin Capital Letter K
L L ASCII: Latin Capital Letter L
M M ASCII: Latin Capital Letter M
N N ASCII: Latin Capital Letter N
O O ASCII: Latin Capital Letter O
P P ASCII: Latin Capital Letter P
Q Q ASCII: Latin Capital Letter Q
R R ASCII: Latin Capital Letter R
S S ASCII: Latin Capital Letter S
T T ASCII: Latin Capital Letter T
U U ASCII: Latin Capital Letter U
V V ASCII: Latin Capital Letter V
W W ASCII: Latin Capital Letter W
X X ASCII: Latin Capital Letter X
Y Y ASCII: Latin Capital Letter Y
Z Z ASCII: Latin Capital Letter Z
[ [ ASCII: opening square bracket
\ \ ASCII: backslash
] ] ASCII: closing square bracket
^ ^ ASCII: spacing circumflex
_ _ ASCII: spacing underscore
` ` ASCII: spacing grave
a a ASCII: Latin Small Letter A
b b ASCII: Latin Small Letter B
c c ASCII: Latin Small Letter C
d d ASCII: Latin Small Letter D
e e ASCII: Latin Small Letter E
f f ASCII: Latin Small Letter F
g g ASCII: Latin Small Letter G
h h ASCII: Latin Small Letter H
i i ASCII: Latin Small Letter I
j j ASCII: Latin Small Letter J
k k ASCII: Latin Small Letter K
l l ASCII: Latin Small Letter L
m m ASCII: Latin Small Letter M
n n ASCII: Latin Small Letter N
o o ASCII: Latin Small Letter O
p p ASCII: Latin Small Letter P
q q ASCII: Latin Small Letter Q
r r ASCII: Latin Small Letter R
s s ASCII: Latin Small Letter S
t t ASCII: Latin Small Letter T
u u ASCII: Latin Small Letter U
v v ASCII: Latin Small Letter V
w w ASCII: Latin Small Letter W
x x ASCII: Latin Small Letter X
y y ASCII: Latin Small Letter Y
z z ASCII: Latin Small Letter Z
{ { ASCII: opening curly bracket
| | ASCII: vertical bar
} } ASCII: closing curly bracket
~ ~ ASCII: tilde
  ASCII: DEL (ctrl DELETE)
?

ASCII: Comma
ƒƒ ASCII: Latin Small Letter F Script
ASCII: Double Prime Quote Mark Low
ASCII: Ellipsis Low
ASCII: Dagger
ASCII: Double Dagger
ˆˆ ASCII: Circumflex
ASCII: Per Mille (thousand) sign
ŠŠ ASCII: Latin Capital Letter S Hacek
ASCII: Less-than sign
ŒŒ ASCII: Latin Capital Letter OE

ŽŽ


ASCII: Opening Single Quotation Mark
ASCII: Closing Single Quotation Mark
ASCII: Opening Double Quotation Mark
ASCII: Closing Double Quotation Mark
ASCII: Bullet
ASCII: Hyphen or En-dash
ASCII: Em-dash
˜˜ ASCII: Tilde accent
ASCII: Trade Mark sign
šš ASCII: Latin Small Letter S Hacek
ASCII: Greater-Than sign
œœ ASCII: Latin Small Letter OE

žž
ŸŸ ASCII: Latin Capital Letter Y Umlaut
 
¡¡ ASCII: Latin Small Letter I
¢¢ ASCII: Cent
££ ASCII: Pound Sterling
¤¤ ASCII: Currency sign
¥¥ ASCII: Yen
¦¦ ASCII: Latin Letter Pipe
§§ ASCII: Section sign
¨¨ ASCII: Umlaut accent
©© ASCII: Copyright sign
ªª ASCII: Superscript Small Letter A
«« ASCII: Left Double-Angle Quotation Mark
¬¬ ASCII: NOT (Math)
­­ ASCII: Hyphen
®® ASCII: Registered Name
¯¯ ASCII: APL Overbar. Macron accent
°° ASCII: Ring accent
±± ASCII: Plus-Minus sign
²² ASCII: Superscript Numeral Two, Squared
³³ ASCII: Superscript Numeral Three, Cubed
´´ ASCII: Grave accent (?)
µµ ASCII: Mu or Micro sign
ASCII: Paragraph or Pilcrow sign
·· ASCII: Middle Dot
¸¸ ASCII: Cedilla Accent
¹¹ ASCII: Superscript Numeral One
ºº ASCII: Degree, Ring Accent
»» ASCII: Right Double-Angle Quotation Mark
¼¼ ASCII: One Quarter
½½ ASCII: One Half
¾¾ ASCII: Three Quarters
¿¿ ASCII: Turned Question Mark
ÀÀ ASCII: Capital Letter A Grave
ÁÁ ASCII: Latin Capital Letter A Acute
ÂÂ ASCII: Latin Capital Letter A Circumflex
ÃÃ ASCII: Latin Capital Letter A Tilde
ÄÄ ASCII: Latin Capital Letter A Umlaut
ÅÅ ASCII: Latin Capital Letter A Ring
ÆÆ ASCII: Latin Capital Letter AE
ÇÇ ASCII: Latin Capital Letter C Cedilla
ÈÈ ASCII: Latin Capital Letter E Grave
ÉÉ ASCII: Latin Capital Letter A Acute
ÊÊ ASCII: Latin Capital Letter E Circumflex
ËË ASCII: Latin Capital Letter E Umlaut
ÌÌ ASCII: Latin Capital Letter I Grave
ÍÍ ASCII: Latin Capital Letter I Acute
ÎÎ ASCII: Latin Capital Letter I Circumflex
ÏÏ ASCII: Latin Capital Letter I Umlaut
ÐÐ ASCII: Latin Capital Letter Eth, D Bar
ÑÑ ASCII: Latin Capital Letter N Tilde
ÒÒ ASCII: Latin Capital Letter O Grave
ÓÓ ASCII: Latin Capital Letter O Acute
ÔÔ ASCII: Latin Capital Letter O Circumflex
ÕÕ ASCII: Latin Capital Letter O Tilde
ÖÖ ASCII: Latin Capital Letter O Umlaut
×× ASCII: Multiplication sign
ØØ ASCII: Greek Letter Phi
ÙÙ ASCII: Latin Capital Letter U Grave
ÚÚ ASCII: Latin Capital Letter U Acute
ÛÛ ASCII: Latin Capital Letter U Circumflex
ÜÜ ASCII: Latin Capital Letter U Umlaut
ÝÝ ASCII: Latin Capital Letter Y Acute
ÞÞ ASCII: Latin Small Letter Thorn
ßß ASCII: Latin Small Letter Sharp S, Ess-Zed
àà ASCII: Latin Small Letter A Grave
áá ASCII: Latin Small Letter A Acute
ââ ASCII: Latin Small Letter A Circumflex
ãã ASCII: Latin Small Letter A Tilde
ää ASCII: Latin Small Letter A Umlaut
åå ASCII: Latin Small Letter A Ring
ææ ASCII: Latin Small Letter AE
çç ASCII: Latin Small Letter C Cedilla
èè ASCII: Latin Small Letter E Grave
éé ASCII: Latin Small Letter E Acute
êê ASCII: Latin Small Letter E Circumflex
ëë ASCII: Latin Small Letter E Umlaut
ìì ASCII: Latin Small Letter I Grave
íí ASCII: Latin Small Letter I Acute
îî ASCII: Latin Small Letter I Circumflex
ïï ASCII: Latin Small Letter I Umlaut
ðð ASCII: Latin Small Letter Eth
ññ ASCII: Latin Small Letter N Tilde
òò ASCII: Latin Small Letter O Grave
óó ASCII: Latin Small Letter O Acute
ôô ASCII: Latin Small Letter O Circumflex
õõ ASCII: Latin Small Letter O Tilde
öö ASCII: Latin Small Letter O Umlaut
÷÷ ASCII: Division sign
øø ASCII: Greek Small Letter Phi
ùù ASCII: Latin Small Letter U Grave
úú ASCII: Latin Small Letter U Acute
ûû ASCII: Latin Small Letter U Circumflex
üü ASCII: Latin Small Letter U Umlaut
ýý ASCII: Latin Small Letter Y Acute
þþ ASCII: Latin Capital Letter Thorn
ÿÿ ASCII: Latin Small Letter Y Umlaut

30 10 2010

How to set jQuery intellisense plugin onto VS 2008 ?


This instruction is compliant with Windows 7.

1- Install VS 2008 SP1 , you can download it for free here.
2- After installation of SP1, restart windows
.
3- Then, install jQuery intellisense hotfix, you can download for free here. (KB958502)
4- Restart VS 2008.
5 - Add jQuery library and its vsdoc file to your application. You can download jQuery here and vsdoc for 1.4.2

Finally we can start to take advantage of jquery by adding reference to your code as shown below;

Share It