I was wondering if someone's written a utility to convert a CSV file to Json using C#. From a previous question on stackoverflow, I'm aware of this nice utility - https://github.com/cparker15/csv-to-json and at the moment I plan to refer to it but an existing C# implementation would be very helpful! Thanks!
If you can use System.Web.Extensions, something like this could work:
var csv = new List<string[]>(); // or, List<YourClass>
var lines = System.IO.File.ReadAllLines(#"C:\file.txt");
foreach (string line in lines)
csv.Add(line.Split(',')); // or, populate YourClass
string json = new
System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
You might have more complex parsing requirements for the csv file and you might have a class that encapsulates the data from one line, but the point is that you can serialize to JSON with one line of code once you have a Collection of lines.
Cinchoo ETL - an open source library available to do the conversion of CSV to JSON easily with few lines of code
For a sample CSV:
Id, Name, City
1, Tom, NY
2, Mark, NJ
3, Lou, FL
4, Smith, PA
5, Raj, DC
Sample code,
string csv = #"Id, Name, City
1, Tom, NY
2, Mark, NJ
3, Lou, FL
4, Smith, PA
5, Raj, DC
";
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv)
.WithFirstLineHeader()
)
{
using (var w = new ChoJSONWriter(sb))
w.Write(p);
}
Console.WriteLine(sb.ToString());
Output JSON:
[
{
"Id": "1",
"Name": "Tom",
"City": "NY"
},
{
"Id": "2",
"Name": "Mark",
"City": "NJ"
},
{
"Id": "3",
"Name": "Lou",
"City": "FL"
},
{
"Id": "4",
"Name": "Smith",
"City": "PA"
},
{
"Id": "5",
"Name": "Raj",
"City": "DC"
}
]
Sample fiddle: https://dotnetfiddle.net/pclnsT
Checkout CodeProject article for some additional help.
UPDATE:
If your CSV file has duplicate column names or no names, please use the below steps to produce the JSON file
string csv = #"Id, Name,
1, Tom, NY
2, Mark, NJ
3, Lou, FL
4, Smith, PA
5, Raj, DC
";
StringBuilder sb = new StringBuilder();
using (var p = ChoCSVReader.LoadText(csv)
.WithField("Id", position: 1)
.WithField("Name", position: 2)
.WithField("City", position: 3)
.WithFirstLineHeader(true)
)
{
using (var w = new ChoJSONWriter(sb))
w.Write(p);
}
Console.WriteLine(sb.ToString());
Sample fiddle: https://dotnetfiddle.net/pP5Du6
Disclaimer: I'm the author of this library.
I used Dictionary and returned json using newtonsoft
public string ConvertCsvFileToJsonObject(string path)
{
var csv = new List<string[]>();
var lines = File.ReadAllLines(path);
foreach (string line in lines)
csv.Add(line.Split(','));
var properties = lines[0].Split(',');
var listObjResult = new List<Dictionary<string, string>>();
for (int i = 1; i < lines.Length; i++)
{
var objResult = new Dictionary<string, string>();
for (int j = 0; j < properties.Length; j++)
objResult.Add(properties[j], csv[i][j]);
listObjResult.Add(objResult);
}
return JsonConvert.SerializeObject(listObjResult);
}
Install Nuget package NewtonSoft.Json
Add reference dll Microsoft.VisualBasic
using System.Linq;
using Newtonsoft.Json;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Project
{
public static class Program
{
public static void Main(string[] args)
{
string CSVpath = #"D:\New Folder\information.csv";
string analyticsData = ReadFile(CSVpath);
}
private static string ReadFile(string filePath)
{
string payload = "";
try
{
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath) && Path.GetExtension(filePath).Equals(".csv", StringComparison.InvariantCultureIgnoreCase))
{
string[] lines = File.ReadAllLines(filePath);
if (lines != null && lines.Length > 1)
{
var headers = GetHeaders(lines.First());
payload = GetPayload(headers, lines.Skip(1));
}
}
}
catch (Exception exp)
{
}
return payload;
}
private static IEnumerable<string> GetHeaders(string data)
{
IEnumerable<string> headers = null;
if (!string.IsNullOrWhiteSpace(data) && data.Contains(','))
{
headers = GetFields(data).Select(x => x.Replace(" ", ""));
}
return headers;
}
private static string GetPayload(IEnumerable<string> headers, IEnumerable<string> fields)
{
string jsonObject = "";
try
{
var dictionaryList = fields.Select(x => GetField(headers, x));
jsonObject = JsonConvert.SerializeObject(dictionaryList);
}
catch (Exception ex)
{
}
return jsonObject;
}
private static Dictionary<string, string> GetField(IEnumerable<string> headers, string fields)
{
Dictionary<string, string> dictionary = null;
if (!string.IsNullOrWhiteSpace(fields))
{
var columns = GetFields(fields);
if (columns != null && headers != null && columns.Count() == headers.Count())
{
dictionary = headers.Zip(columns, (x, y) => new { x, y }).ToDictionary(item => item.x, item => item.y);
}
}
return dictionary;
}
public static IEnumerable<string> GetFields(string line)
{
IEnumerable<string> fields = null;
using (TextReader reader = new StringReader(line))
{
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(","); fields = parser.ReadFields();
}
}
return fields;
}
}
}
Taking only a dependency on Newtonsoft.Json, here's a helper method given an array of CSV lines, the first one being the header.
public static IEnumerable<JObject> CsvToJson(IEnumerable<string> csvLines)
{
var csvLinesList = csvLines.ToList();
var header = csvLinesList[0].Split(',');
for (int i = 1; i < csvLinesList.Count; i++)
{
var thisLineSplit = csvLinesList[i].Split(',');
var pairedWithHeader = header.Zip(thisLineSplit, (h, v) => new KeyValuePair<string, string>(h, v));
yield return new JObject(pairedWithHeader.Select(j => new JProperty(j.Key, j.Value)));
}
}
I use ChoETL:
using ChoETL;
using System.IO;
public class FromCSVtoJSON
{
public FromCSVtoJSON()
{
}
public void convertFile(string inputFile, string outputFile)
{
using (var writer = new ChoJSONWriter(outputFile))
{
using (var reader = new ChoCSVReader(inputFile).WithFirstLineHeader())
{
writer.Write(reader);
}
}
}
}
From that same SO answer, there is a link to this post.
CsvToJson extention method
/// <summary>
/// Converts a CSV string to a Json array format.
/// </summary>
/// <remarks>First line in CSV must be a header with field name columns.</remarks>
/// <param name="value"></param>
/// <returns></returns>
public static string CsvToJson(this string value)
{
// Get lines.
if (value == null) return null;
string[] lines = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length < 2) throw new InvalidDataException("Must have header line.");
// Get headers.
string[] headers = lines.First().SplitQuotedLine(new char[] { ',' }, false);
// Build JSON array.
StringBuilder sb = new StringBuilder();
sb.AppendLine("[");
for (int i = 1; i < lines.Length; i++)
{
string[] fields = lines[i].SplitQuotedLine(new char[] { ',', ' ' }, true, '"', false);
if (fields.Length != headers.Length) throw new InvalidDataException("Field count must match header count.");
var jsonElements = headers.Zip(fields, (header, field) => string.Format("{0}: {1}", header, field)).ToArray();
string jsonObject = "{" + string.Format("{0}", string.Join(",", jsonElements)) + "}";
if (i < lines.Length - 1)
jsonObject += ",";
sb.AppendLine(jsonObject);
}
sb.AppendLine("]");
return sb.ToString();
}
There appears to be an issue with where some methods called within the above extension live (see the comments of the original blog post), but it should get you most of the way there.
EDIT Here is another SO answer about splitting a CSV line. You could use one of the suggested regex solutions to create your own SplitQuotedLine method:
public static string SplitQuotedLine(this string value, char separator, bool quotes) {
// Use the "quotes" bool if you need to keep/strip the quotes or something...
var s = new StringBuilder();
var regex = new Regex("(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
foreach (Match m in regex.Matches(value)) {
s.Append(m.Value);
}
return s.ToString();
}
I did not test the above, so forgive me if I made any errors.
Also, it would appear that Zip is a LINQ extension method, so that takes care of that problem.
Here's mine.. It can parse 9k CSV records in centuries. LOL
class CSVTOJSON
{
public string ConvertToJSON()
{
string json = string.Empty;
string csv = string.Empty;
using (StreamReader reader = new StreamReader("data.csv"))
{
csv = reader.ReadToEnd();
}
string[] lines = csv.Split(new string[] { "\n" }, System.StringSplitOptions.None);
if (lines.Length > 1)
{
// parse headers
string[] headers = lines[0].Split(',');
StringBuilder sbjson = new StringBuilder();
sbjson.Clear();
sbjson.Append("[");
// parse data
for (int i = 1; i < lines.Length; i++)
{
if (string.IsNullOrWhiteSpace(lines[i])) continue;
if (string.IsNullOrEmpty(lines[i])) continue;
sbjson.Append("{");
string[] data = lines[i].Split(',');
for (int h = 0; h < headers.Length; h++)
{
sbjson.Append(
$"\"{headers[h]}\": \"{data[h]}\"" + (h < headers.Length - 1 ? "," : null)
);
}
sbjson.Append("}" + (i < lines.Length - 1 ? "," : null));
}
sbjson.Append("]");
json = sbjson.ToString();
}
return json;
}
}
But it works.
console log:
Converting CSV to JSON
CSV has 9486 data
Total duration converting CSV to JSON: 00:00:00.0775373
Small variation to the solution by bc3tech i.e. avoiding external dependencies (on Newtonsoft.Json), and instead using System.Text.Json (dotnet core 3+)
public static IEnumerable<string> CsvToJson(string fileName, char delim = '|')
{
var lines = File.ReadLines(fileName);
var hdr = new List<string>(lines.First().Trim().Split(delim));
foreach (var l in lines.Skip(1).Where(l => (l.Trim() != String.Empty)))
{
var val = l.Trim().Split(delim);
var ds = hdr.Zip(val, (k, v) => new { k, v }).ToDictionary(x => x.k, x => x.v);
yield return JsonSerializer.Serialize(ds);
}
}
I can see most people simply assume parsing CSV file is to simply split comma delimiter between each column, but the following format is still a valid CSV
"aaa","bbb","ccc"
"z, z",yyy,xxx
There is a nice class hidden within Microsoft.VisualBasic.FileIO to handle CSV file format correctly. I combine this with JSON.NET came up with the solution.
public static string? CsvToJson(string input, string delimiter)
{
using (TextFieldParser parser = new TextFieldParser(
new MemoryStream(Encoding.UTF8.GetBytes(input))))
{
parser.Delimiters = new string[] { delimiter };
string[]? headers = parser.ReadFields();
if (headers == null) return null;
string[]? row;
string comma = "";
var sb = new StringBuilder((int)(input.Length * 1.1));
sb.Append("[");
while ((row = parser.ReadFields()) != null)
{
var dict = new Dictionary<string, object>();
for (int i = 0; row != null && i < row.Length; i++)
dict[headers[i]] = row[i];
var obj = JsonConvert.SerializeObject(dict);
sb.Append(comma + obj);
comma = ",";
}
return sb.Append("]").ToString();
}
}
Usage
var str = #"Header1,""Header,,2 "",Data3
1,444.00, ""Liang, Jerry""
0,""5,550"",Jerry
";
var json = CsvToJson(str, ",");
Result
[
{
"Header1": "1",
"Header,,2": "444.00",
"Data3": "Liang, Jerry"
},
{
"Header1": "0441",
"Header,,2": "5,550",
"Data3": "Jerry"
}
]
I looked for the answer for this question finally i solved it by using Dictionary
public static void CreateJsonFromCSV()
{
string path = "C:\\Users\\xx\\xx\\xx\\xx\\lang.csv";
string textFilePath = path;
const Int32 BufferSize = 128;
using (var fileStream = File.OpenRead(textFilePath))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
{
String line;
Dictionary<string, string> jsonRow = new Dictionary<string, string>();
while ((line = streamReader.ReadLine()) != null)
{
string[] parts = line.Split(',');
string key_ = parts[0];
string value = parts[1];
if (!jsonRow.Keys.Contains(key_))
{
jsonRow.Add(key_, value );
}
}
var json = new JavaScriptSerializer().Serialize(jsonRow);
string path_ = "C:\\XX\\XX\\XX\\XX\\XX.csv";
File.WriteAllText(path_, json);
}
}
Make sure you add the below in web.config before you do parse large csv files.
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
Try this:
StreamReader sr = new StreamReader(filePath);
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
string[] csv = line.Split(',');
var dictionary = new Dictionary<string, string>();
dictionary.Add("dispatching_base_number",csv[0]);
dictionary.Add("available_vehicles", csv[1]);
dictionary.Add("vehicles_in_trips", csv[2]);
dictionary.Add("Cancellations", csv[3]);
string jsonN = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dictionary);
Console.WriteLine("Sending message: {0}",jsonN);
}
Try this and convert CSV to JSON object:
public static List<object> CsvToJson( string body, string[] column ) {
if ( string.IsNullOrEmpty( body ) ) return null;
string[] rowSeparators = new string[] { "\r\n" };
string[] rows = body.Split( rowSeparators, StringSplitOptions.None );
body = null;
if ( rows == null || ( rows != null && rows.Length == 0 ) ) return null;
string[] cellSeparator = new string[] { "," };
List<object> data = new List<object>( );
int clen = column.Length;
rows.Select( row => {
if ( string.IsNullOrEmpty( row ) ) return row;
string[] cells = row.Trim( ).Split( cellSeparator, StringSplitOptions.None );
if ( cells == null ) return row;
if ( cells.Length < clen ) return row;
Dictionary<object, object> jrows = new Dictionary<object, object>( );
for ( int i = 0; i < clen; i++ ) {
jrows.Add( column[i], cells[i]?.Trim( ) );
}
data.Add( jrows );
return row;
} ).ToList( );
rowSeparators = null; rows = null;
cellSeparator = null;
return data;
}
var data = CsvToJson("csv_input_str", new string[]{ "column_map" })
string jsonStr = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize( data );
First, load the csv file into datatable and serialize it to Json document. It uses OLEDB Provider that can parse the csv wisely,
Courtesy to Jim Scott, https://stackoverflow.com/a/1050278/6928056
Courtesy to K_B, https://stackoverflow.com/a/2979938/6928056
using System.Data;
using System.Data.OleDb;
using System.Globalization;
using System.IO;
using Newtonsoft.Json;
static string ConvertCsvToJson(string path, bool isFirstRowHeader)
{
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = #"SELECT * FROM [" + fileName + "]";
using(OleDbConnection connection = new OleDbConnection(
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using(OleDbCommand command = new OleDbCommand(sql, connection))
using(OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
var dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
return JsonConvert.SerializeObject(dataTable, Formatting.Indented);
}
}
Simple method to convert flat csv file to a collection of simple json formatted objects. Works with files with header row on the first line. Part of this method was found somewhere else on SO.
Add reference to Microsoft.VisualBasic.
using Microsoft.VisualBasic.FileIO;
public static StringBuilder ReadCsv()
{
var path = #"X:\...\input.csv";
using (TextFieldParser csvParser = new TextFieldParser(path))
{
csvParser.CommentTokens = new string[] { "#" };
//Remember to use your own separator
csvParser.SetDelimiters(new string[] { ";" });
csvParser.HasFieldsEnclosedInQuotes = false;
StringBuilder json = new StringBuilder();
string[] colNames = new string[0];
string[] fields = new string[0];
json.Append("[");
int counter = 0;
while (!csvParser.EndOfData)
{
if (counter == 0)
{
//Read properies' names
colNames = csvParser.ReadFields();
counter++;
Console.WriteLine($"{colNames.Length} columns detected!");
}
else
{
// Read current line fields, pointer moves to the next line.
// Read the properties' values
fields = csvParser.ReadFields();
json.Append("{");
for (int i = 0; i < colNames.Length; i++)
{
json.Append($"\"{colNames[i]}\":{TryParse(fields[i])}");
if (i != colNames.Length - 1)
{
json.Append(",");
}
}
json.Append("},");
Console.WriteLine($"Writing record nr.: {counter}");
counter++;
}
}
json.Length--; //Remove trailing comma
json.Append("]");
return json;
}
}
string TryParse(string s)
{
if (string.IsNullOrEmpty(s)) return "null";
//Remember to set your decimal character here!
if (s.Contains('.'))
{
double dResult;
//This works on my computer, could be different on your machine
if (double.TryParse(s, NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture, out dResult))
return dResult.ToString(CultureInfo.InvariantCulture);
}
else
{
int intResult;
if (int.TryParse(s, out intResult))
return intResult.ToString(CultureInfo.InvariantCulture);
}
return "\"" + s + "\"";
}
This should give you a simple list of json objects.
If you are looking for a C# only solution this might work for you. I recently face the same issue and created this method to overcome.
public static string ConvertToJsonStructure(string csvDataAsString, char delimiter = ',', bool hasHeaders = true)
{
var output = string.Empty;
if (string.IsNullOrEmpty(csvDataAsString))
return "{}";
var rows = csvDataAsString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var headers = hasHeaders ? rows[0].Split(delimiter) : null;
var colCount = headers?.Length;
var jsonStart = "{";
var jsonEnd = "}";
if (rows.Length > (1 + (hasHeaders ? 1 : 0)))
{
jsonStart = "[";
jsonEnd = "]";
}
output = output + jsonStart;
int startIndex = hasHeaders ? 1 : 0;
for (int i = startIndex; i < rows.Length; i++)
{
var cols = rows[i].Split(delimiter);
if (colCount == null)
colCount = cols.Length;
var tempJson = "{";
for (int j = 0; j < colCount.Value; j++)
{
if (hasHeaders)
tempJson = tempJson + $"\"{headers[j]}\":";
var isNumber = Regex.IsMatch(cols[j], #"^\d + $");
var val = isNumber ? cols[j] : $"\"{cols[j]}\"";
tempJson = tempJson + val;
if (j < colCount.Value - 1)
tempJson = tempJson + ",";
}
tempJson = tempJson + "}";
if (i < rows.Length - 1)
tempJson = tempJson + ",";
output = output + tempJson;
}
output = output + jsonEnd;
return output;
}
}
You need to pass your content as string to this method and you can do something like this to read your csv
var csvAsString = File.ReadAllText("TestCsv.csv");
var result = CsvToJson.ConvertToJsonStructure(csvAsString);
Related
E2739158012008-10-01O9918107NPF7547379999010012008-10-0100125000000
E2739158PU0000-00-00 010012008-10-0100081625219
E3180826011985-01-14L9918007NPM4927359999010011985-01-1400005620000
E3180826PU0000-00-00 020011985-01-14000110443500021997-01-1400000518799
E3292015011985-01-16L9918007NPM4927349999010011985-01-1600003623300
I have this flat file and I need to group this based on the 2nd position to 8th position
example(2739158/3180826/3292015) and write to another flat file.
So the data Starting with 'E' should Repeat in the single line along with that group field(2nd to 8th Position in the start) and I should take the 9th Position after 'E'
Also I need to replace Empty space with ('*' star)
For example
1st Line
2739158**E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000*****E**012008-10-01O9918107NPF7547379999010012008-10-0100125000000
2nd Line
3180826**E**011985-01-14L9918007NPM4927359999010011985-01-1400005620000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000**E**011985-01-14L9918007NPM4927359999010011985-01-140000562000***
3rd Line
3292015**E**011985-01-16L9918007NPM4927349999010011985-01-1600003623300****
Can we do this in Stream reader c#, please?
Any help would be highly appreciated.The file size is more than 285 MB so it it good to read through Stream Reader?
Thanks
#jdweng: thanks very much for your input. i tried somehow without grouping and it works as expected.Thanks everyone who tried to solve the issue.
string sTest= string.Empty; List<SortLines> lines = new List<SortLines>();
List<String> FinalLines = new List<String>();
using (StreamReader sr = new StreamReader(#"C:\data\Input1.txt))
{
sr.ReadLine();
string line = "";
while (!sr.EndOfStream)
{
line = sr.ReadLine();
//line = line.Trim();
if (line.Length > 0)
{
line = line.Replace(" ", "*");
SortLines newLine = new SortLines()
{
key = line.Substring(1, 7),
line = line
};
if (sTest != newLine.key)
{
//Add the Line Items to String List
sOuterLine = sTest + sOneLine;
FinalLines.Add(sOuterLine);
string sFinalLine = newLine.line.Remove(1, 7);
string snewLine = newLine.key + sFinalLine;
sTest = snewLine.Substring(0, 7);
//To hold the data for the 1st occurence
sOtherLine = snewLine.Remove(0, 7);
bOtherLine = true;
string sKey = newLine.key;
lines.Add(newLine);
}
else if (sTest == newLine.key)
{
string sConcatLine = String.Empty;
string sFinalLine = newLine.line.Remove(1, 7);
//Check if 1st Set
if (bOtherLine == true)
{
sOneLine = sOtherLine + sFinalLine;
bOtherLine = false;
}
//If not add subsequent data
else
{
sOneLine = sOneLine + sFinalLine;
}
//Check for the last line in the flat file
if (sr.Peek() == -1)
{
sOuterLine = sTest + sOneLine;
FinalLines.Add(sOuterLine);
}
}
}
}
}
//Remove the Empty List
FinalLines.RemoveAll(x => x == "");
StreamWriter srWriter = new StreamWriter(#"C:\data\test.txt);
foreach (var group in FinalLines)
{
srWriter.WriteLine(group);
}
srWriter.Flush();
srWriter.Close();
Try code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string INPUT_FILENAME = #"c:\temp\test.txt";
const string OUTPUT_FILENAME = #"c:\temp\test1.txt";
static void Main(string[] args)
{
List<SortLines> lines = new List<SortLines>();
StreamReader reader = new StreamReader(INPUT_FILENAME);
string line = "";
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line.Length > 0)
{
line = line.Replace(" ", "*");
SortLines newLine = new SortLines() { key = line.Substring(2, 7), line = line };
lines.Add(newLine);
}
}
reader.Close();
var groups = lines.GroupBy(x => x.key);
StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);
foreach (var group in groups)
{
foreach (SortLines sortLine in group)
{
writer.WriteLine(sortLine.line);
}
}
writer.Flush();
writer.Close();
}
}
public class SortLines : IComparable<SortLines>
{
public string line { get; set; }
public string key { get; set; }
public int CompareTo(SortLines other)
{
return key.CompareTo(other);
}
}
}
I am using CSV Helper library to produce CSV files for the user to
to populate and upload into the system. My issue is that the WriteHeader method just writes the attributes of a class with names like "PropertyValue", which is not user friendly. Is there a method I can use to make the text produced user friendly but is still able to successfully map the class to the files data?
My code looks like the following:
public ActionResult UploadPropertyCSV(HttpPostedFileBase file)
{
List<PropertyModel> properties = new List<PropertyModel>();
RIMEDb dbContext = new RIMEDb();
bool success = false;
foreach (string requestFiles in Request.Files)
{
if (file != null && file.ContentLength > 0 && file.FileName.EndsWith(".csv"))
{
using(StreamReader str = new StreamReader(file.InputStream))
{
using(CsvHelper.CsvReader theReader = new CsvHelper.CsvReader(str))
{
while (theReader.Read())
{
RIMUtil.PropertyUploadCSVRowHelper row = new RIMUtil.PropertyUploadCSVRowHelper()
{
UnitNumber = theReader.GetField(0),
StreetNumber = theReader.GetField(1),
StreetName = theReader.GetField(2),
AlternateAddress = theReader.GetField(3),
City = theReader.GetField(4)
};
Property property = new Property();
property.UnitNumber = row.UnitNumber;
property.StreetNumber = row.StreetNumber;
property.StreetName = row.StreetName;
property.AlternateAddress = row.AlternateAddress;
property.City = dbContext.PostalCodes.Where(p => p.PostalCode1 == row.PostalCode).FirstOrDefault().City;
dbContext.Properties.Add(property);
try
{
dbContext.SaveChanges();
success = true;
}
catch(System.Data.Entity.Validation.DbEntityValidationException ex)
{
success = false;
RIMUtil.LogError("Ptoblem validating fields in database. Please check your CSV file for errors.");
}
catch(Exception e)
{
RIMUtil.LogError("Error saving property to database. Please check your CSV file for errors.");
}
}
}
}
}
}
return Json(success);
}
I'm wondering if theres some metadata tag or something I can put on top of each attribute in my PropertyUploadCSVRowHelper class to put the text I want produced in the file
Thanks in advance
Not sure if this existed 2 years ago but now, we can change the property/column name by using the following attribute function:
[CsvHelper.Configuration.Attributes.Name("Column/Field Name")]
Full code:
using CsvHelper;
using System.Collections.Generic;
using System.IO;
namespace Test
{
class Program
{
class CsvColumns
{
private string column_01;
[CsvHelper.Configuration.Attributes.Name("Column 01")] // changes header/column name Column_01 to Column 01
public string Column_01 { get => column_01; set => column_01 = value; }
}
static void Main(string[] args)
{
List<CsvColumns> csvOutput = new List<CsvColumns>();
CsvColumns rows = new CsvColumns();
rows.Column_01 = "data1";
csvOutput.Add(rows);
string filename = "test.csv";
using (StreamWriter writer = File.CreateText(filename))
{
CsvWriter csv = new CsvWriter(writer);
csv.WriteRecords(csvOutput);
}
}
}
}
This might not be answering your question directly as you said you wanted to use csvhelper, but if you're only writing small size files (this is a simple function that I use to generate csv. Note, csvhelper will be much better for larger files as this is just building a string and not streaming the data.
Just customise the columns array in the code below variable to suit your needs.
public string GetCsv(string[] columns, List<object[]> data)
{
StringBuilder CsvData = new StringBuilder();
//add column headers
string[] s = new string[columns.Length];
for (Int32 j = 0; j < columns.Length; j++)
{
s[j] = columns[j];
if (s[j].Contains("\"")) //replace " with ""
s[j].Replace("\"", "\"\"");
if (s[j].Contains("\"") || s[j].Contains(" ")) //add "'s around any string with space or "
s[j] = "\"" + s[j] + "\"";
}
CsvData.AppendLine(string.Join(",", s));
//add rows
foreach (var row in data)
{
for (int j = 0; j < columns.Length; j++)
{
s[j] = row[j] == null ? "" : row[j].ToString();
if (s[j].Contains("\"")) //replace " with ""
s[j].Replace("\"", "\"\"");
if (s[j].Contains("\"") || s[j].Contains(" ")) //add "'s around any string with space or "
s[j] = "\"" + s[j] + "\"";
}
CsvData.AppendLine(string.Join(",", s));
}
return CsvData.ToString();
}
Here is a fiddle example of how to use it: https://dotnetfiddle.net/2WHf6o
Good luck.
I have a CSV file but the delimiter is a semi colon ; and each column is enclosed with double quotes. There are also occurrences of ; in some values such as & amp;
I am using TextFieldParser to parse the file. This is the sample data:
"A001";"RT:This is a tweet"; "http://www.whatever.com/test/module & amp;one"
For the above example , I am getting more columns/fields than what I should get.
Field[0] = "A001"
Field[1] = "RT:This is a tweet"
Field[2] = "http://www.whatever.com/test/module&"
Field[3] = "one"
This is my code. What changes need to be done to handle such scenario?
using (var parser = new TextFieldParser(fileName))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(";");
parser.TrimWhiteSpace = true;
parser.HasFieldsEnclosedInQuotes = false;
int rowIndex = 0;
PropertyInfo[] properties = typeof(TwitterData).GetProperties();
while (parser.PeekChars(1) != null)
{
var cleanFieldRowCells = parser.ReadFields().Select(
f => f.Trim(new[] { ' ', '"' }));
var twitter = new TwitterData();
int index = 0;
foreach (string c in cleanFieldRowCells)
{
string str = c;
if (properties[index].PropertyType == typeof(DateTime))
{
string twitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
DateTime createdAt = DateTime.ParseExact(str, twitterDateTemplate, new System.Globalization.CultureInfo("en-AU"));
properties[index].SetValue(twitter, createdAt);
}
else
{
properties[index].SetValue(twitter, str);
}
index++;
}
}
-Alan-
Using the two sample strings you have above and setting the HasFieldsEnclosedInQuotes property to true works for me.
string LINES = #"
""A001"";""RT:This is a tweet""; ""http://www.whatever.com/test/module&one""
""A001"";""RT: Test1 ; Test2"";""test.com"";
";
using (var sr = new StringReader(LINES))
{
using (var parser = new TextFieldParser(sr))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(";");
parser.TrimWhiteSpace = true;
parser.HasFieldsEnclosedInQuotes = true;
while (parser.PeekChars(1) != null)
{
var cleanFieldRowCells = parser.ReadFields().Select(
f => f.Trim(new[] { ' ', '"' })).ToArray();
Console.WriteLine("New Line");
for (int i = 0; i < cleanFieldRowCells.Length; ++i)
{
Console.WriteLine(
"Field[{0}] = [{1}]", i, cleanFieldRowCells[i]
);
}
Console.WriteLine("{0}", new string('=', 40));
}
}
}
OUTPUT:
New Line
Field[0] = [A001]
Field[1] = [RT:This is a tweet]
Field[2] = [http://www.whatever.com/test/module&one]
========================================
New Line
Field[0] = [A001]
Field[1] = [RT: Test1 ; Test2]
Field[2] = [test.com]
Field[3] = []
========================================
Want to create a generic text file parser in c# for any find of text file.Actually i have 4 application all 4 getting input data from txt file format but text files are not homogeneous in nature.i have tried fixedwithdelemition.
private static DataTable FixedWidthDiliminatedTxtRead()
{
string[] fields;
StringBuilder sb = new StringBuilder();
List<StringBuilder> lst = new List<StringBuilder>();
DataTable dtable = new DataTable();
ArrayList aList;
using (TextFieldParser tfp = new TextFieldParser(testOCC))
{
tfp.TextFieldType = FieldType.FixedWidth;
tfp.SetFieldWidths(new int[12] { 2,25,8,12,13,5,6,3,10,11,10,24 });
for (int col = 1; col < 13; ++col)
dtable.Columns.Add("COL" + col);
while (!tfp.EndOfData)
{
fields = tfp.ReadFields();
aList = new ArrayList();
for (int i = 0; i < fields.Length; ++i)
aList.Add(fields[i] as string);
if (dtable.Columns.Count == aList.Count)
dtable.Rows.Add(aList.ToArray());
}
}
return dtable;
}
but i feel its very rigid one and really varies application to application making it configgurable .any better way ..
tfp.SetFieldWidths(new int[12] { 2,25,8,12,13,5,6,3,10,11,10,24 });
File nature :
Its a report kind of file .
position of columns are very similar
row data of file id different .
I get this as a reference
http://www.codeproject.com/Articles/11698/A-Portable-and-Efficient-Generic-Parser-for-Flat-F
any other thoughts ?
If the only thing different is the field widths, you could just try sending the field widths in as a parameter:
private static DataTable FixedWidthDiliminatedTxtRead(int[] fieldWidthArray)
{
string[] fields;
StringBuilder sb = new StringBuilder();
List<StringBuilder> lst = new List<StringBuilder>();
DataTable dtable = new DataTable();
ArrayList aList;
using (TextFieldParser tfp = new TextFieldParser(testOCC))
{
tfp.TextFieldType = FieldType.FixedWidth;
tfp.SetFieldWidths(fieldWidthArray);
for (int col = 1; col < 13; ++col)
dtable.Columns.Add("COL" + col);
while (!tfp.EndOfData)
{
fields = tfp.ReadFields();
aList = new ArrayList();
for (int i = 0; i < fields.Length; ++i)
aList.Add(fields[i] as string);
if (dtable.Columns.Count == aList.Count)
dtable.Rows.Add(aList.ToArray());
}
}
return dtable;
}
If you will have more logic to grab the data, you might want to consider defining an interface or abstract class for a GenericTextParser and create concrete implementations for each other file.
Hey I made one of these last week.
I did not write it with the intentions of other people using it so I appologize in advance if its not documented well but I cleaned it up for you. ALSO I grabbed several segments of code from stack overflow so I am not the original author of several pieces of this.
The places you need to edit are the path and pathout and the seperators of text.
char[] delimiters = new char[]
So it searches for part of a word and then grabs the whole word. I used a c# console application for this.
Here you go:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace UniqueListofStringFinder
{
class Program
{
static void Main(string[] args)
{
string path = #"c:\Your Path\in.txt";
string pathOut = #"c:\Your Path\out.txt";
string data = "!";
Console.WriteLine("Current Path In is set to: " + path);
Console.WriteLine("Current Path Out is set to: " + pathOut);
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Input String to Search For:");
Console.Read();
string input = Console.ReadLine();
// Delete the file if it exists.
if (!File.Exists(path))
{
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
System.IO.StreamReader file = new System.IO.StreamReader(path);
List<string> Spec = new List<string>();
using (StreamReader sr = File.OpenText(path))
{
while (!file.EndOfStream)
{
string s = file.ReadLine();
if (s.Contains(input))
{
char[] delimiters = new char[] { '\r', '\n', '\t', ')', '(', ',', '=', '"', '\'', '<', '>', '$', ' ', '#', '[', ']' };
string[] parts = s.Split(delimiters,
StringSplitOptions.RemoveEmptyEntries);
foreach (string word in parts)
{
if (word.Contains(input))
{
if( word.IndexOf(input) == 0)
{
Spec.Add(word);
}
}
}
}
}
Spec.Sort();
// Open the stream and read it back.
//while ((s = sr.ReadLine()) != null)
//{
// Console.WriteLine(s);
//}
}
Console.WriteLine();
StringBuilder builder = new StringBuilder();
foreach (string s in Spec) // Loop through all strings
{
builder.Append(s).Append(Environment.NewLine); // Append string to StringBuilder
}
string result = builder.ToString(); // Get string from StringBuilder
Program a = new Program();
data = a.uniqueness(result);
int i = a.writeFile(data,pathOut);
}
public string uniqueness(string rawData )
{
if (rawData == "")
{
return "Empty Data Set";
}
List<string> dataVar = new List<string>();
List<string> holdData = new List<string>();
bool testBool = false;
using (StringReader reader = new StringReader(rawData))
{
string line;
while ((line = reader.ReadLine()) != null)
{
foreach (string s in holdData)
{
if (line == s)
{
testBool = true;
}
}
if (testBool == false)
{
holdData.Add(line);
}
testBool = false;
// Do something with the line
}
}
int i = 0;
string dataOut = "";
foreach (string s in holdData)
{
dataOut += s + "\r\n";
i++;
}
// Write the string to a file.
return dataOut;
}
public int writeFile(string dataOut, string pathOut)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(pathOut);
file.WriteLine(dataOut);
file.Close();
}
catch (Exception ex)
{
dataOut += ex.ToString();
return 1;
}
return 0;
}
}
}
private static DataTable FixedWidthTxtRead(string filename, int[] fieldWidths)
{
string[] fields;
DataTable dtable = new DataTable();
ArrayList aList;
using (TextFieldParser tfp = new TextFieldParser(filename))
{
tfp.TextFieldType = FieldType.FixedWidth;
tfp.SetFieldWidths(fieldWidths);
for (int col = 1; col <= fieldWidths.length; ++col)
dtable.Columns.Add("COL" + col);
while (!tfp.EndOfData)
{
fields = tfp.ReadFields();
aList = new ArrayList();
for (int i = 0; i < fields.Length; ++i)
aList.Add(fields[i] as string);
if (dtable.Columns.Count == aList.Count) dtable.Rows.Add(aList.ToArray());
}
}
return dtable;
}
Here's what I did:
I built a factory for the type of processor needed (based on file type/format), which abstracted the file reader.
I then built a collection object that contained a set of triggers for each field I was interested in (also contained the property name for which this field is destined). This settings collection is loaded in via an XML configuration file, so all I need to change are the settings, and the base parsing process can react to how the settings are configured. Finally I built a reflection wrapper wherein once a field is parsed, the corresponding property on the model object is set.
As the file flowed through, the triggers for each setting evaluated each lines value. When it found what it was set to find (via pattern matching, or column length values) it fired and event that bubbled up and set a property on the model object. I can show some pseudo code if you're interested. It needs some work for efficiency's sake, but I like the concept.
I have a comma-separated string. How can I convert it into line-break-separated format. My string looks like this:
red,yellow,green,orange,pink,black,white
And needs to be formatted to in this way:
red
yellow
green
orange
pink
black
white
Here is my code:
public static string getcolours()
{
List<string> colours = new List<string>();
DBClass db = new DBClass();
DataTable allcolours = new DataTable();
allcolours = db.GetTableSP("kt_getcolors");
for (int i = 0; i < allcolours.Rows.Count; i++)
{
string s = allcolours.Rows[i].ItemArray[0].ToString();
string missingpath = "images/color/" + s + ".jpg";
if (!FileExists(missingpath))
{
colours.Add(s);
}
}
string res = string.Join(", ", colours);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"F:\test.txt", true))
{
file.WriteLine(res);
}
return res;
}
res = res.Replace(',','\n');
This should work.
you can try:
string colours = "red,yellow,green,orange,pink,black,white";
string res = string.Join(Environment.NewLine, colours.Split(','));
Or much simpler version would be:
string res2 = colours.Replace(",", Environment.NewLine);
Don't string join, just writeline the colors and next return with join on \n
public static string getcolours()
{
List<string> colours = new List<string>();
DBClass db = new DBClass();
DataTable allcolours = new DataTable();
allcolours = db.GetTableSP("kt_getcolors");
for (int i = 0; i < allcolours.Rows.Count; i++)
{
string s = allcolours.Rows[i].ItemArray[0].ToString();
string missingpath = "images/color/" + s + ".jpg";
if (!FileExists(missingpath))
{
colours.Add(s);
}
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"F:\test.txt", true))
{
foreach(string color in colours)
{
file.WriteLine(color);
}
}
return string.Join("\n", colours);;
}
var s = "red,yellow,green,orange,pink,black,white";
var r = string.Join(Environment.NewLine, s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));