+using System;\r
+using System.Collections;\r
+using System.Collections.Generic;\r
+using System.Linq;\r
+using System.Text;\r
+using System.IO;\r
+\r
+namespace MLSFileTrimmer\r
+{\r
+ public class CSVTrimmer\r
+ {\r
+ private String filename = String.Empty;\r
+ private Hashtable mlsFields = new Hashtable();\r
+\r
+ public CSVTrimmer(ThirdCouncelorMLSFilesMLSFile xmlData)\r
+ {\r
+ this.filename = xmlData.Name;\r
+\r
+ foreach (String s in xmlData.MLSField)\r
+ {\r
+ this.mlsFields.Add(s, -1);\r
+ }\r
+ }\r
+\r
+ public void ExtractData(StreamReader readFile, StreamWriter outFile)\r
+ {\r
+ string line;\r
+ string[] row;\r
+\r
+ // get headers and create mapping\r
+ if ((line = readFile.ReadLine()) != null)\r
+ {\r
+ row = line.Split('"');\r
+ for (int x = 0; x < row.Length; x++)\r
+ {\r
+ if (!(row[x].Equals(",")) && !(row[x].Equals("")))\r
+ {\r
+ if (this.mlsFields.ContainsKey(row[x]) || this.mlsFields.ContainsKey("*"))\r
+ {\r
+ this.mlsFields[row[x]] = x;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ // write headers in new file\r
+ ICollection headers = this.mlsFields.Keys;\r
+ StringBuilder sb = new StringBuilder();\r
+ foreach (string s in headers)\r
+ {\r
+ int index = Convert.ToInt32(this.mlsFields[s]);\r
+ if (index >= 0)\r
+ {\r
+ sb.Append("\"");\r
+ sb.Append(s);\r
+ sb.Append("\",");\r
+ }\r
+ }\r
+ //sb.Remove(sb.Length - 1, 1); // remove trailing comma\r
+ outFile.WriteLine(sb.ToString());\r
+\r
+ // only write specified fields in new file\r
+ while ((line = readFile.ReadLine()) != null)\r
+ {\r
+ sb.Remove(0, sb.Length);\r
+ row = line.Split('"');\r
+ foreach (string s in headers)\r
+ {\r
+ int index = Convert.ToInt32(this.mlsFields[s]);\r
+ if (index >= 0)\r
+ {\r
+ sb.Append("\"");\r
+ sb.Append(row[index]);\r
+ sb.Append("\",");\r
+ }\r
+ }\r
+ //sb.Remove(sb.Length - 1, 1); // remove trailing comma\r
+ outFile.WriteLine(sb.ToString());\r
+ }\r
+ }\r
+ }\r
+}\r