Are you looking for customized advices? Click here to contact the expert!
cghersi
cghersi
Reputation Top 1%
Cristiano Ghersi
Great passion for Software Engineering, mostly coding in Java and C#, I enjoy creating modular and reusable code for everybody!
191 Snippets  (1st place)
Published
13 Channels
Created
54 Channels
Following
yesterday at 5:45:38 PM
Last Visit
May 5, 2012
Registered
6302 points  (1st place)
Reputation
Top 1% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Master Code Generator
Guru Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Master Publisher
Guru Publisher
Master Topic Creator
Guru Topic Creator
Junior Topic Hub
Serious Topic Hub
Senior Topic Hub
Master Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Senior Team Manager
Junior Influencer
Serious Influencer
Senior Influencer
Junior Commenter
Serious Commenter
Senior Commenter
Master Commenter
Junior Judge
Serious Judge
Senior Judge
Master Judge
Junior Popular Coder
Serious Popular Coder
Senior Popular Coder
Master Popular Coder
Guru Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Snip2Coder
Serious Snip2Coder
Senior Snip2Coder
Junior Geek Aggregator
Serious Geek Aggregator
Senior Geek Aggregator
Junior Famous Coder

Recent Snippets See all snippets by cghersi

public by cghersi modified Thursday at 12:41:00 AM  1  3  1

How to query some values inside XML in SQL Server

This query relies on 'value' operator applied on XML fields. You can directly transform the resulting value into a particular type with the second parameter of this function.
WITH XMLNAMESPACES ('http://www.snip2code.com/namespaces/s2c' AS s2c)
SELECT  field1, field2
FROM    MyTable
WHERE   MyXMLField.value('/s2c:root/fieldName != ""', 'bit') = 1

public by cghersi modified Thursday at 12:29:40 AM  255  5  0

Query XML fields' content in MS SQL Server

This is the right syntax to perform XQuery queries for XML columns in a SQL Server table
SELECT MyField.query('
  declare namespace s2c="http://www.snip2code.com/namespaces/s2c";
	(/s2c:myroot/myelem/fieldIwant[1]/text())')
FROM  MyTable

public by cghersi modified Thursday at 12:24:24 AM  1  3  0

How to create an asynchronous task in C#

This is a simple way to create a task that will run asynchronously with respect to the current thread
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
  log.Info("This will run async");
});

public by cghersi modified Nov 4, 2014  4  3  1

Calculate the occurrences of words in Rss feed

Creates a dictionary with keys=words and values=occurrences of that word in the given Rss (specified by the URL). The caller should specify the maximum number of items in the returning dictionary and, optionally, the minimum length of the words that will be added to the output.
public static Dictionary<string, int> ComputeWordsOccurrencesInRss(string url, int maxItems, int minLenOfAcceptedWords = 1)
{
  Dictionary<string, int> occurrences = new Dictionary<string, int>();

  IList<RssReader.RssItem> singleRssResult = RssReader.ReadRss(url, maxItems, true);
  string[] splitted = null;
  foreach (RssReader.RssItem rss in singleRssResult)
  {
    List<string> words = new List<string>();

    //split title in words:
    splitted = StringHelper.SplitIntoWords(rss.Title);
    if (splitted != null)
      words.AddRange(splitted);

    //split summary in words:
    splitted = StringHelper.SplitIntoWords(rss.Summary);
    if (splitted != null)
      words.AddRange(splitted);

    //compute occurrences of words:
    for (int w = 0; w < words.Count; w++)
    {
      string word = words[w].ToLower();
      if (word.Length < minLenOfAcceptedWords)
        continue;
      if (occurrences.ContainsKey(word))
        occurrences[word]++;
      else
        occurrences.Add(word, 1);
    }
  }

  return occurrences;
}

public by cghersi modified Nov 4, 2014  2  3  2

Read the content of an rss feed

Retrieves the RSS Item at the specified URL and prepares a list of RSS Items with tile, summary and id. The caller should specify the maximum number of items to download and can decide to strip HTML from title and summary.
public class RssReader
{
  public class RssItem
  {
    public string Title { get; set; }
    public string Summary { get; set; }
    public string Id { get; set; }
  }
  
  /// <summary>
  /// Requires System.ServiceModel to be added as a reference
  /// </summary>
  /// <param name="url"></param>
  /// <param name="maxItems"></param>
  public static IList<RssItem> ReadRss(string url, int maxItems, bool stripHtml = false)
  {
    IList<RssItem> result = new List<RssItem>();
    XmlReader reader = XmlReader.Create(url);
    SyndicationFeed feed = null;
    try
    {
      feed = SyndicationFeed.Load(reader);
    }
    catch
    {
      return result;
    }
    finally
    {
      reader.Close();
    }
  
    int totCount = 0;
    foreach (SyndicationItem item in feed.Items)
    {
      if (totCount++ > maxItems)
        break;
      string subject = (item.Title == null ? "" : item.Title.Text);
      string summary = (item.Summary == null ? "" : item.Summary.Text);
      if (stripHtml)
        summary = HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(HtmlHelper.RemoveHtmlTag(summary, "div"), "a"), "img"), "table"), "br");
      string id = item.Id;
      result.Add(new RssItem() { Title = subject, Summary = summary, Id = id});
    }
    return result;
  }
}