# Chicago Gov-Job Average Salary Bubble Charts.

According to this data : Current Employee Names, Salaries, and Position Titles on City Of Chicago Data Portal https://data.cityofchicago.org/;

The Chicago government employees 32,160 employees, and pays approximately \$2,429,980,941.36 in annual salaries.

CLICK ON Pictures FOR Focus. (temporary courtesy of imgur)

All Employees.

Let’s remove distinct Position-Titles with Less Then 10 Employees, and the position title of Police Officer from the data.

(20 or more), and, (200 or less); employee counts within a position title.

(10 or more), and,( 50 or less); employee counts within a position title.

C#

```int counter = 0; string line; Dictionary&lt;string, int&gt; dCount = new Dictionary&lt;string, int&gt;(); Dictionary&lt;string, decimal&gt; dSalTotal = new Dictionary&lt;string, decimal&gt;(); Dictionary&lt;string, LinkedList&gt; dDeps = new Dictionary&lt;string, LinkedList&gt;(); System.IO.StreamReader file = new System.IO.StreamReader("C:\\AAAChicagoEmployees\\Current_Employee_Names__Salaries__and_Position_Titles (1).csv"); while ((line = file.ReadLine()) != null) { string[] parts = line.Split(','); try { string name = parts[2]; name = Regex.Replace(name, "'", ""); string dep = parts[3]; decimal sal = decimal.Parse(parts[4].TrimStart('\$') ); if ( dCount.ContainsKey(name ) ) { int a = dCount[name]; a += 1; dCount[name] = a; decimal b = dSalTotal[name]; b += sal; dSalTotal[name] = b; LinkedList c = dDeps[name]; c.AddLast(dep); dDeps[name] = c; } else { dCount.Add(name, 1); dSalTotal.Add(name, sal); LinkedList newDepList = new LinkedList(); newDepList.AddLast(dep); dDeps.Add(name, newDepList); } } catch (Exception x) { //header line } counter++; } file.Close(); Dictionary&lt;string, decimal&gt; dAveOrder = new Dictionary&lt;string, decimal&gt;(); foreach (KeyValuePair&lt;string, int&gt; item in dCount.OrderBy(key =&gt; key.Value)) { if (item.Value &gt;= 10 &amp;&amp; item.Value &lt;= 50) { decimal aveSal = dSalTotal[item.Key] / item.Value; dAveOrder.Add(item.Key, aveSal); } } string json = ""; foreach (KeyValuePair&lt;string, decimal&gt; item in dAveOrder.OrderBy(key =&gt; key.Value)) { Hashtable frequencyHash = new Hashtable(); LinkedList uniqueList = new LinkedList(); foreach (var element in dDeps[item.Key]) { if (uniqueList.Contains(element)) { int elementCount = int.Parse(frequencyHash[element].ToString()); elementCount++; frequencyHash[element] = elementCount; } else { uniqueList.AddLast(element); frequencyHash.Add(element, 1); } } int max = 0; string sMaxDep = ""; foreach (string element in frequencyHash.Keys) { int iDep = int.Parse(frequencyHash[element].ToString()); if (iDep &gt; max) { max = iDep; sMaxDep = element; } } if (item.Key == "POLICE OFFICER") { } else { json += "[ '" + item.Key + "', " + dCount[item.Key] + " , " + item.Value + " ,'" + Regex.Replace(sMaxDep, "'", "") + "' , " + item.Value + " ],\r\n"; } } System.IO.StreamWriter fileWrite = new System.IO.StreamWriter("c:\\AAAChicagoEmployees\\data.txt"); fileWrite.WriteLine(json); fileWrite.Close();```

# DataViz: Visualizing Chicago Narcotics Crime Data.

In this post, I will give a semi-detailed overview of how to make an animated heat map over a time range.  This is my first attempt at making an animated heat map.

I will be revisiting Chicago crime statistics from City of Chicago Data Portal 2001-2013

This dataset reflects reported incidents of crime (with the exception of murders where data exists for each victim) that occurred in the City of Chicago from 2001 to present, minus the most recent seven days.Data is extracted from the Chicago Police Department’s CLEAR (Citizen Law Enforcement Analysis and Reporting) system. Read More about the dataset here

On 11/28/2013 I downloaded a csv from chicago’s data portal named Crimes_-_2001_to_present.csv, its size was 1,050,688 KB

Here is an example of how I would parse out all coordinates of narcotics violations for the month of January in 2013.

Lets test the coordinate outputs by appending them into a google maps heatmap.

Okay, now I’m going to manually make one of these images (with static center and size) for each month from January 2012 to October 2013. Next, I wrote a quick c# application using ImageMagick to programmatically crop each Browser screenshot.

```string[] files = Directory.GetFiles(@"c:\__FULLCHICAGOCRIME\Months\ScreenShots"); foreach (string file in files) { string ext = Path.GetExtension(file); if (ext == ".png") { string filenameNoExt = Path.GetFileNameWithoutExtension(file); string filenameExt = Path.GetFileName(file); Directory.CreateDirectory(@"c:\ImageConversion\" + filenameNoExt); Process imProcess = new Process(); string im_command = file + @" -crop 800x800+500+300 c:\__FULLCHICAGOCRIME\Months\ScreenShots\cropped\" + filenameNoExt + "_cropped.png"; imProcess.StartInfo.UseShellExecute = false; imProcess.StartInfo.RedirectStandardOutput = true; imProcess.StartInfo.FileName = @"c:\__FULLCHICAGOCRIME\Months\ScreenShots\convert"; imProcess.StartInfo.Arguments = im_command; imProcess.Start(); imProcess.WaitForExit(); } }```

Now I have the frames for my animation.

Lastly, I append all these images into a movie using Windows Movie Maker, and add some Beethoven as background music.

# C#: Programmatically Playing Prince of Persia.

In this post I will programmatically pass level 1 in Prince of Persia using c#.

This game came out in 1989, it is now freeware.

In order to run MS DOS Prince of Persia(In 2013 win7 era), I had to download DOSBOX.
Dos Box Website: http://www.dosbox.com/

I used this input simulator in my application:
Windows input Simulator http://inputsimulator.codeplex.com/.

C# Code used.

```using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; using WindowsInput;```
```namespace Prince { public partial class Form1 : Form { [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool AllocConsole(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { AllocConsole(); } [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); private void button1_Click(object sender, EventArgs e) { Process[] localAll = Process.GetProcesses(); IntPtr calculatorHandle = FindWindow(null, "DOSBox 0.74, Cpu speed: 3000 cycles, Frameskip 0, Program: PRINCE"); if (calculatorHandle == IntPtr.Zero) return; SetForegroundWindow(calculatorHandle); Thread.Sleep(500); if (SetForegroundWindow(calculatorHandle)) { commandPrince(VirtualKeyCode.NUMPAD6, 3700); commandPrince(VirtualKeyCode.SHIFT, 2000); commandPrince(VirtualKeyCode.NUMPAD4, 4200); Thread.Sleep(500); commandPrince(VirtualKeyCode.NUMPAD8, 2000); Thread.Sleep(1400);   //.... This ^ is a small sample of commands I give to Prince //.... Full level 1 commands are below in the post.   } } public void standingJumpForw(VirtualKeyCode key, VirtualKeyCode key2, int ms) { Console.WriteLine("Standing Forward Jump"); InputSimulator.SimulateKeyDown(key); Thread.Sleep(100); InputSimulator.SimulateKeyDown(key2); Thread.Sleep(ms); InputSimulator.SimulateKeyUp(key2); InputSimulator.SimulateKeyUp(key); Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up "); } public void commandClibUp(VirtualKeyCode key, VirtualKeyCode key2, int ms) { Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down "); InputSimulator.SimulateKeyDown(key); Thread.Sleep(ms); InputSimulator.SimulateKeyDown(key2); Thread.Sleep(500); InputSimulator.SimulateKeyUp(key2); InputSimulator.SimulateKeyUp(key); Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up "); } public void commandPrince(VirtualKeyCode key, int ms) { Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " down "); InputSimulator.SimulateKeyDown(key); Thread.Sleep(ms); InputSimulator.SimulateKeyUp(key); Console.WriteLine(DateTime.Now.ToString("H:mm:ss") + " " + key.ToString() + " up "); }   public void runningJump(VirtualKeyCode key, VirtualKeyCode key2, int ms) { Console.WriteLine("Running Jump "); InputSimulator.SimulateKeyDown(key); Thread.Sleep(ms); InputSimulator.SimulateKeyDown(key2); Thread.Sleep(550); InputSimulator.SimulateKeyUp(key2); InputSimulator.SimulateKeyUp(key); } public void runningJumpFast(VirtualKeyCode key, VirtualKeyCode key2, int ms) { Console.WriteLine("Running Jump Fast"); InputSimulator.SimulateKeyDown(key); Thread.Sleep(ms); InputSimulator.SimulateKeyDown(key2); Thread.Sleep(550); InputSimulator.SimulateKeyUp(key2); InputSimulator.SimulateKeyUp(key); } } }```

# C#: Parsing Horses.

My goal with this post is to parse out all the horses which came in 1st to 3rd place from each race that took place at Arlington Park this season.

The website http://www.arlingtonpark.com/racing-handicapping/equibase/charts provides a calendar which links to the result file for each race date from the past. Clicking on a particular date will link you to a pdf which contains various statistics.

So lets download all of these programmatically. To achieve this, I wrote a small program to iterate over dates in the uri, requesting every race date pdf file.

Then, I save them into a folder on my c: drive called \HORSEDATA\. I also, wait 1 -3 second before each request, alternate the web client’s header, and delete the downloaded file if it is too small (No Race happened on that date).

```DateTime Today = DateTime.Now; int randomWait = 0; Random random = new Random(); for (int i = 0; i &lt; 200; i++) { randomWait = random.Next(1000, 3000); Thread.Sleep(randomWait); Console.WriteLine("Downloading : " + Today.ToString("d")); Today = Today.AddDays( -1 ); try { using (WebClient client = new WebClient()) { client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.370"+i+";)"); client.DownloadFile("http://www.equibase.com/premium/eqbPDFChartPlus.cfm?RACE=A&amp;BorP=P&amp;TID=AP&amp;CTRY=USA&amp;DT=" + Today.ToString("d") + "&amp;DAY=D&amp;STYLE=EQB", @"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf"); } } catch (Exception x) {Console.WriteLine("No Race on this Date.");} FileInfo f = new FileInfo(@"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf"); long s1 = f.Length; Console.WriteLine(s1.ToString()); if (s1 &lt; 4000) File.Delete(@"C:\HORSEDATA\\" + Today.ToString("MM-dd-yy") + ".pdf"); }```

Now your \HORSEDATA\ folder should contain the proper pdfs.

Next, read through all the race date PDFs. For each PDF, I want to get the names of horses which came in 1st to 3rd place, and count up the amount of times each unique horse at least showed.

Using iTextSharp library to read the PDFs and count up my horses.

```using iTextSharp; using iTextSharp.text.pdf; using System.IO; using iTextSharp.text.pdf.parser; using System.Text.RegularExpressions;```
```Console.BufferHeight = 6000; Dictionary&lt;string, int&gt; dictHorseShowCount = new Dictionary&lt;string, int&gt;(); LinkedList uniqueHorse = new LinkedList(); string[] filePaths = Directory.GetFiles(@"c:\HORSEDATASET\"); foreach (string file in filePaths) { Console.WriteLine("Reading : " + file); PdfReader reader = new PdfReader(file); StringWriter output = new StringWriter(); for (int i = 1; i &lt;= reader.NumberOfPages; i++) output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy())); Regex regPattern = new Regex(@"Total WPS Pool(.*?)Copyright", RegexOptions.Singleline); MatchCollection matchX = regPattern.Matches(output.ToString()); foreach (Match match in matchX) { Match match2 = Regex.Match(match.ToString(), @"Win Place Show(.*?)Wager Type", RegexOptions.Singleline); if (match2.Success) { string [] lineparts = Regex.Split(match2.ToString(),"\n"); for( int i = 1; i &lt; lineparts.Length - 1; i++ ) { string horseOnly = Regex.Replace(lineparts[i].ToString(), "[^a-zA-Z -]", ""); if (!uniqueHorse.Contains(horseOnly.Trim())) uniqueHorse.AddLast(horseOnly.Trim()); if (dictHorseShowCount.ContainsKey(horseOnly.Trim())) { int existing_int = dictHorseShowCount[horseOnly.Trim()]; existing_int += 1; dictHorseShowCount[horseOnly.Trim()] = existing_int; } else dictHorseShowCount[horseOnly.Trim()] = 1; } } } } var sortedDict = (from entry in dictHorseShowCount orderby entry.Value ascending select entry) .ToDictionary(pair =&gt; pair.Key, pair =&gt; pair.Value); foreach (string key in sortedDict.Keys) Console.WriteLine(key + " " + sortedDict[key]); Console.WriteLine(""); Console.WriteLine("Distinct Horse Showed : " + uniqueHorse.Count); Console.WriteLine("");```

Output

# C#: Parse a Sentence Containing a Word from Text using Regular Expressions.

Recently, I had received an email from someone asking me how to obtain all sentences containing a specific word. So, I made this quick post. The code below shows how to use regular expressions to parse all sentences from text, then check to see if the sentence contains a specific word.

```//Look for sentences containing the word "bank" string word = "bank"; //Text String string fulltext = @"Starting in the early 1960s federal banking regulators interpreted provisions of the Glass–Steagall Act to permit commercial banks and especially commercial bank affiliates to engage in an expanding list and volume of securities activities. By the time the affiliation restrictions in the Glass–Steagall Act were repealed through the Gramm–Leach–Bliley Act of 1999 (GLBA), many commentators argued Glass–Steagall was already “dead.” Most notably, Citibank’s 1998 affiliation with Salomon Smith Barney, one of the largest US securities firms, was permitted under the Federal Reserve Board’s then existing interpretation of the Glass–Steagall Act. President Bill Clinton publicly declared ""the Glass–Steagall law is no longer appropriate."" Many commentators have stated that the GLBA’s repeal of the affiliation restrictions of the Glass–Steagall Act was an important cause of the late-2000s financial crisis. Some critics of that repeal argue it permitted Wall Street investment banking firms to gamble with their depositors' money that was held in affiliated commercial banks. Others have argued that the activities linked to the financial crisis were not prohibited (or, in most cases, even regulated) by the Glass–Steagall Act. Commentators, including former President Clinton in 2008 and the American Bankers Association in January 2010, have also argued that the ability of commercial banking firms to acquire securities firms (and of securities firms to convert into bank holding companies) helped mitigate the financial crisis.";   //Match Collection for every sentence MatchCollection matchSentences = Regex.Matches(fulltext, @"([A-Z][^\.!?]*[\.!?])"); //Alternative pattern : @"(\S.+?[.!?])(?=\s+|\$)"   //counter for sentences. int foundSentenceWithWord = 0; foreach (Match sFound in matchSentences) { foreach (Capture capture in sFound.Captures) { string current_sentence = capture.Value; //if you don't want to match for words like 'bank'er or 'bank'ing //use the word boundary "\b" //change this pattern to @"\b"+word+@"\b" Match matchWordInSentence = Regex.Match(capture.Value, word, RegexOptions.IgnoreCase); if (matchWordInSentence.Success) { Console.WriteLine("Sentence Found Containing '" + word+"' :"); Console.WriteLine(current_sentence); Console.WriteLine(); foundSentenceWithWord++; } } } Console.WriteLine(); Console.WriteLine("Found " + foundSentenceWithWord + " Sentences Containing the word '" + word + "'"); Console.WriteLine();```

Output:

# C#: Split GIF frames into PNGs using ImageMagick

How to split apart a GIF image frame by frame, and save each frame as PNG into a sub-folder.

To achieve this I will be using ImageMagick
http://www.imagemagick.org/

I created a folder on my C: Drive called
/ImageConversion/

Place your GIFs into the ImageConversion Folder, also copy convert.exe into there as well. So the folder should look like this:

Execute this code:

```//get all files in folder string[] files = Directory.GetFiles(@"c:\ImageConversion\");   //loop through each file foreach (string file in files) { //check if .GIF string ext = Path.GetExtension(file); if (ext == ".gif") { Console.WriteLine("Splitting : "+file); //Get the name of the file without extension string filenameNoExt = Path.GetFileNameWithoutExtension(file); //Get the file name with extention string filenameExt = Path.GetFileName(file); Console.WriteLine(); //Create a Sub Directory with the same as the GIF's filename Directory.CreateDirectory(@"c:\ImageConversion\"+filenameNoExt); Process imProcess = new Process(); //Arguments string im_command = @"c:\ImageConversion\" + filenameExt + @" -scene 1 +adjoin -coalesce c:\ImageConversion\" + filenameNoExt + @"\" + filenameNoExt + ".png"; imProcess.StartInfo.UseShellExecute = false; imProcess.StartInfo.RedirectStandardOutput = true; imProcess.StartInfo.FileName = @"c:\ImageConversion\convert"; imProcess.StartInfo.Arguments = im_command; imProcess.Start(); imProcess.WaitForExit(); } }```

That’s it, you’re done.
Now the ImageConversion folder should be populated with a folder for every gif containing every frame as an individual png.

# C#: Programmatically download all Images from a website and save them locally.

For this example, I will be downloading all the .gifs from a specific page on imgur. Then, I will save them into a folder called _Images.

What the website looks like:

Below is C# code that will:
-parse out specific image links from the returned html string using regular expressions

```WebClient wchtml = new WebClient(); string htmlString = wchtml.DownloadString("http://imgur.com/a/GPlx4"); int mastercount = 0; Regex regPattern = new Regex(@"http://i.imgur.com/(.*?)alt=""", RegexOptions.Singleline); MatchCollection matchImageLinks = regPattern.Matches(htmlString );   foreach (Match img_match in matchImageLinks) { string imgurl = img_match.Groups[1].Value.ToString(); Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase); MatchCollection ms = regx.Matches(imgurl); foreach (Match m in ms) { Console.WriteLine("Downloading.. " + m.Value); mastercount++; try { WebClient wc = new WebClient(); wc.DownloadFile(m.Value, @"C:\_Images\bg_" + mastercount + ".gif"); Thread.Sleep(1000); } catch (Exception x) { Console.WriteLine("Failed to download image."); } break; } }```

Output: C:\_Images

# C#: Parse a website and save specific content as XML.

In this tutorial I will show you how to iterate through a website using a WebClient,  Save each page’s content into a string, parse it using regular expressions, and save it as XML.

I will be parsing airplane data from airliners.net

Here is a picture of what the website looks like.

Get started with a .NET C# Console application and create a WebClient.

Use the URL and Output the html returned.

```WebClient wc = new WebClient(); string htmlString = wc.DownloadString("http://www.airliners.net/aircraft-data/stats.main?id=2"); Console.WriteLine(htmlString);```

Lets take a look at both  the console output and the source of the page to make sure we got the right html back.

Console and Page Source

Success, we found the Airplane title!  Now lets see if we can parse it, and only it, from the entire string of html.

Try to see if there is a pattern that’s unique to what you need.

```WebClient wc = new WebClient(); string htmlString = wc.DownloadString("http://www.airliners.net/aircraft-data/stats.main?id=2"); Match mTitle = Regex.Match(htmlString, @"<center><h1>(.*?)</h1>"); if (mTitle.Success) { string airplaneTitle = mTitle.Groups[1].Value; Console.WriteLine(airplaneTitle); }```

Success.

Now lets get Country of Origin and the other categories.

```string airplaneCountry = ""; Match mCountry = Regex.Match(htmlString, @"<b>Country of origin</font>(.*?)<p>", RegexOptions.Singleline); if (mCountry.Success) { airplaneCountry = mCountry.Groups[1].Value; Console.WriteLine(airplaneCountry); } Console.WriteLine("***************************************************************"); //manicure the pattern string //Replace everything before the last (greater than) sign with and empty string airplaneCountry = Regex.Replace(airplaneCountry, "(.*?)>", "").Trim(); Console.WriteLine(airplaneCountry);```

Now I am tired of typing, so lets make a method for everything else.

```static void Main(string[] args) {   WebClient wc = new WebClient(); string htmlString = wc.DownloadString("http://www.airliners.net/aircraft-data/stats.main?id=2");   Console.WriteLine("Name :"+ parsePattern(@"<center><h1>(.*?)</h1>",htmlString)); Console.WriteLine("Country :" + parsePattern(@"<b>Country of origin</font>(.*?)<p>", htmlString)); Console.WriteLine("Powerplants:" + parsePattern(@"<b>Powerplants</font>(.*?)<p>", htmlString)); Console.WriteLine("Performance:" + parsePattern(@"<b>Performance</font>(.*?)<p>", htmlString)); Console.WriteLine("Weights :" + parsePattern(@"<b>Weights</font>(.*?)<p>", htmlString)); Console.WriteLine("Dimentions :" + parsePattern(@"<b>Dimensions</font>(.*?)<p>", htmlString)); Console.WriteLine("Capacity :" + parsePattern(@"<b>Capacity</font>(.*?)<p>", htmlString)); Console.WriteLine("Type :" + parsePattern(@"<b>Type</font>(.*?)<p>", htmlString)); Console.WriteLine("Production :" + parsePattern(@"<b>Production</font>(.*?)<p>", htmlString));   //lol :D Damn this History! //Console.WriteLine(parsePattern(@"<b>History</font>(.*?)<table border=0 cellpadding=1 cellspacing=0 >", htmlString)); //History breaks my method pattern cause there's too many <p>'s //Doing History Manually, Due to Different Pattern. Match mHistory = Regex.Match(htmlString, @"<b>History</font>(.*?)<table border=0 cellpadding=1 cellspacing=0 >", RegexOptions.Singleline); if (mHistory.Success) { string strContent = mHistory.Groups[1].Value; //Google your problems "C# regex to remove html" - thanks stackoverflow //Get @"<[^>]*>" strContent = Regex.Replace(strContent, @"<[^>]*>", "").Trim(); Console.WriteLine("History : " + strContent); } }```
```public static string parsePattern(string pat, string htmlString) { Match mCategory = Regex.Match(htmlString, @pat, RegexOptions.Singleline); if (mCategory.Success) { string strContent = mCategory.Groups[1].Value; if (strContent.Contains('>')) { strContent = Regex.Replace(strContent, "(.*?)>", "").Trim(); return strContent; } else return strContent; } return ""; }```

Now lets save everything into xml :D
I Created a Folder in my C: Drive called C:\Airplanes\
Iterate through each page and save the content.

Here’s the full Code to do the Whole Website:

```static void Main(string[] args) { //Since we don't want to hold a glock up to the webserver's dome piece: //Let's pretend to be a human who goes to each page within 1 - 3 seconds randomly. WebClient wc = new WebClient(); int pageCount = 1; int randomWait = 0; Random random = new Random(); while (true) { randomWait = random.Next(1000, 3000); Thread.Sleep(randomWait); string htmlString = wc.DownloadString("http://www.airliners.net/aircraft-data/stats.main?id=" + pageCount); ReadAndAppend(htmlString); pageCount++; Console.WriteLine("Saving :" + pageCount); } } public static void ReadAndAppend(string htmlString) { string name = parsePattern(@"<center><h1>(.*?)</h1>", htmlString); string country = parsePattern(@"<b>Country of origin</font>(.*?)<p>", htmlString); string power = parsePattern(@"<b>Powerplants</font>(.*?)<p>", htmlString); string perf = parsePattern(@"<b>Performance</font>(.*?)<p>", htmlString); string lb = parsePattern(@"<b>Weights</font>(.*?)<p>", htmlString); string dim = parsePattern(@"<b>Dimensions</font>(.*?)<p>", htmlString); string cap = parsePattern(@"<b>Capacity</font>(.*?)<p>", htmlString); string type = parsePattern(@"<b>Type</font>(.*?)<p>", htmlString); string prod = parsePattern(@"<b>Production</font>(.*?)<p>", htmlString); string hist = ""; Match mHistory = Regex.Match(htmlString, @"<b>History</font>(.*?)<table border=0 cellpadding=1 cellspacing=0 >", RegexOptions.Singleline); if (mHistory.Success) { string strContent = mHistory.Groups[1].Value; strContent = Regex.Replace(strContent, @"<[^>]*>", "").Trim(); hist = strContent; } //make the xml string xmlString = "<plane>"; xmlString += " <name>" + name + "</name>\r\n"; xmlString += " <country>" + country + "</country>\r\n"; xmlString += " <power>" + power + "</power>\r\n"; xmlString += " <perf>" + perf + "</perf>\r\n"; xmlString += " <lb>" + lb + "</lb>\r\n"; xmlString += " <dim>" + dim + "</dim>\r\n"; xmlString += " <cap>" + cap + "</cap>\r\n"; xmlString += " <type>" + type + "</type>\r\n"; xmlString += " <prod>" + prod + "</prod>\r\n"; xmlString += " <hist>" + hist + "</hist>\r\n"; xmlString += "</plane>\r\n\r\n";   //Show me the saves and count Console.WriteLine(xmlString);   //save to C:\Airplanes\ StreamWriter streamWrite; streamWrite = File.AppendText("C:\\Airplanes\\airData.xml"); streamWrite.WriteLine(xmlString); streamWrite.Close(); }   public static string parsePattern(string pat, string htmlString) { Match mCategory = Regex.Match(htmlString, @pat, RegexOptions.Singleline); if (mCategory.Success) { string strContent = mCategory.Groups[1].Value; if (strContent.Contains('>')) { strContent = Regex.Replace(strContent, "(.*?)>", "").Trim(); return strContent; } else return strContent; } return ""; }```

Heres the XML file Created.

# C#: How to get correlation coefficient of two arrays Correl()

This simple example shows you how to get the correlation coefficient of two arrays.  Microsoft Excel and OpenOffice Calc has a function for this called CORREL() :

```//Two arrays double[] array1 = { 3, 2, 4, 5, 6 }; double[] array2 = { 9, 7, 12, 15, 17 };   double[] array_xy = new double[array1.Length]; double[] array_xp2 = new double[array1.Length]; double[] array_yp2 = new double[array1.Length]; for (int i = 0; i &lt; array1.Length; i++) array_xy[i] = array1[i] * array2[i]; for (int i = 0; i &lt; array1.Length; i++) array_xp2[i] = Math.Pow(array1[i], 2.0); for (int i = 0; i &lt; array1.Length; i++) array_yp2[i] = Math.Pow(array2[i], 2.0); double sum_x = 0; double sum_y = 0; foreach (double n in array1) sum_x += n; foreach (double n in array2) sum_y += n; double sum_xy = 0; foreach (double n in array_xy) sum_xy += n; double sum_xpow2 = 0; foreach (double n in array_xp2) sum_xpow2 += n; double sum_ypow2 = 0; foreach (double n in array_yp2) sum_ypow2 += n; double Ex2 = Math.Pow(sum_x, 2.00); double Ey2 = Math.Pow(sum_y, 2.00);   double Correl = (array1.Length * sum_xy - sum_x * sum_y) / Math.Sqrt((array1.Length * sum_xpow2 - Ex2) * (array1.Length * sum_ypow2 - Ey2));   Console.WriteLine("CORREL : "+ Correl);```

# C#: Project Euler Solution to Problem 27

http://projecteuler.net/problem=27

```static void Main(string[] args) { //n² + an + b int maxprimes = 0; int maxproduct = 0; for (int a = 0; a < 1000; a++) { for (int b = 0; b < 1000; b++) { int prime = countPrime(a, b); if (maxprimes < prime) { maxprimes = prime; maxproduct= (-a*b ); } } } Console.WriteLine(maxproduct); }   public static int countPrime(int a, int b) { int count = 0; int n = 0; while (true) { double result = Math.Pow(n, 2.00) - (a * n) + b; if (isPrime((int)result) && result >= 0) count++; else return count; n++; } }   public static bool isPrime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i < n; ++i) { if ((n % i) == 0) return false; } return true; }```