Is there any way to Read/Write object to .csv - c#

I am looking for some thing like .Net utility which allows you to export data/object to .csv and similarly read that data. Same like we have a concept of xmlSerialization.
Thanx

Have you tried SimpleCSV? (codeplex project)

I think it would be quicker and easier to write your own parser for this purpose. All you need to do is create a text file and append it with all the parameters of your object separated by a delimiter (";").
When you are reading it back in you will know the order of the parameters that are coming in and how they are separated so you can create a new object and convert the strings into their appropriate types and apply these values to your new object.

You can easily write your own serializer/parser using the String.Join and String.Split methods.

Related

character to use when splitting strings in visual c#?

Ok, I'm racking my brains over this one. It's pretty simple though (I think).
I'm currently creating a text file as a comma separated string of values.
Later, I read in that file data and then use the .split function to split the data by commas.
I discovered that sometimes one of the description fields in the data conatins an embedded comma, which ends up throwing the split command off.
Is there any special character I could use that could pretty much guarantee wouldn't be in the data, or is there a better way to accomplish this? Thanks!
// Initial Load
fullString = fileName + "," + String.Join(",", fieldValues);
// Access later
String[] valuesArray = myString.Split(',');
Short answer, there's no "simple" way to do it using Split. The best you can hope for is to set the deliminator as something cooky that wouldn't ever get used (but even that's not a guarantee).
The simple method would be to used something like CsvHelper (get it through Nuget) or any of the other dozen or so packages that are designed for parsing CSV.

Finding specific parts of strings containing certain letters/symbols and then creating a reference to those parts

Currently I am storing data in form of jsons (strings) on a database. As jsons contain quotation marks though and the database I am using is unable to store quotation marks in this form: " it converts all quotation marks (like this one :") to "
Unity will therefor not allow me to deserialize the json anymore as it now looks somewhat like this:
{"coins":0,"level":0,"kills":0,"deaths":0,"xp":0.0}
instead of like this:
{"coins":0,"level":0,"kills":0,"deaths":0,"xp":0.0}
Obviously a possible solution to this would be to find all the parts of my json string containing ", storing a reference to these parts and then converting all of those parts to a simple "
Therefore I would ask you how I would go about doing this.
You can use String.replace(""","\"") and than String.split, but maybe you need to think about moving to a database that supports JSONs, like mongodb. Other direction to solve this: have you tried placing the " as \"?
The Database is doing a good job by encoding the text for you thereby preventing Hacks!! It is simply doing text encoding for you.
All you have to do is Decode the text before using it. If there are chances that double quote is part of the data then you should be careful while reverse converting the encoded text. Refer to this MSDN resource Anti-Cross Site Scripting Library to get better insight into topic

How to compare 2 web.configs by content in C# using powershell?

I want to compare 2 web.config files by content, so that the difference is shown in terms of line number. I don't want to use Compare-Object as it compares line by line, which is not exactly the proper method for comparison.
Perhaps XmlDiff can help. It understands XML and generates a difference.
Have you considered to Serialize the config files? You can even generate classes with xsd.exe and use the objects in your program as you like.
You are looking for compare two web.config it means you wants two xml files, so you have to think that
You can use Xdocument object, Initialize two diffrunt XDocument object and load both xml respectivly
ref this answer

Extract an integer from a string and replace

I'd like to create a program that will go through a file (XML), find out a specific tag, will extract the integer from it and add a value to it, some like a replacement.
I know about the StreamWrite function but I'd need some help with the extracting-adding value-replacing thingy. I also would need to use the "\d+"
You can work with System.Xml.Serialization wich allow you to convert xml to objects and objects to xml and so you can do what you want.
More information

Inserting a Line at a specific place in a txt using .net

The process I currently use to insert a string into a text file is to read the file and modify it as I write the file out again which seems to be how everyone is doing it.
Since .net has such a diverse library I was wondering if there was a specific command for inserting a string at a specific line in a txt file.
I would imagine it would look something like this:
dim file as file
file.open("filePath")
file.insert(string, location) 'this isn't actually an option, I tried
No, there's nothing specifically to do that in .NET.
You can do it very simply if you're happy to read all the lines of text in:
var lines = File.ReadAllLines("file.txt").ToList();
lines.Insert(location, text);
File.WriteAllLines("file.txt", lines);
It would be more efficient in terms of memory to write code to copy a line at a time, inserting the new line at the right point while you copy, but that would be more work. If you know your file will be small, I'd go for the above.
You could simply read all the text into a string, then use the string insert method, e.g.
File.WriteAllText("file.txt", File.ReadAllText("file.txt").Insert(startIndex, "value"));

Categories