Hi ,
I have text file format like below. I am saving into csv file in c#.
//need to ignore Personnel Clearance Pair line,Credential line in the text file.
Access.text file:
Name:,4th Floor, IT
Personnel Clearance Pair
Clearance name:,iT
Credential
Card number:,1234
Name:,Graham, John
Personnel Clearance Pair
Clearance name:,Temps
Credential
Card number:,23489
I am looping throught the text file like below
try
{
System.IO.TextWriter Summarytw = System.IO.File.CreateText(sOutputFilePath + sOutFileName_DST);
string line1 = string.Empty;
string strColumnname = string.Empty;
strColumnname = "Name,Clearance name,Card number";
string[] arr = strColumnname.Split(',');
int a = arr.Length;
for (int y = 0; y < arr.Length; y++)
{
line += arr[y].ToString() + ",";
}
Summarytw.WriteLine(line);
using (System.IO.StreamReader sr = new System.IO.StreamReader(sInputPath + sInFile))
{
while ((line = sr.ReadLine()) != null)
{
if ((line.Contains("Name")) || (line.Contains("Clearance name")) || (line.Contains("Card number")))
{
string s = line.ToString();
string pattern = ":,";
string strdelimiter = ",";
string[] substrings = Regex.Split(line, pattern);
for (int i = 1; i < substrings.Length; i++)
{
string value = substrings[1].ToString();
if (i == substrings.Length)
{
strconcat += " " + value + " ";
}
else
{
strconcat += "\" " + value + "\"";
strconcat += strdelimiter;
}
}
}
if (line.StartsWith("Card number"))
{
strconcat += Environment.NewLine;
}
}
Summarytw.WriteLine(strconcat);
}
}
for the above line in Access.txt code it was working fine.
but if the data is like below
Name:,lori, Julie
Personnel Clearance Pair
Clearance name:,Access Full Time Employees
Credential
Name:,Badge 43511, Visitor
Personnel Clearance Pair
Credential
Card number:,43511
if there is no card number or clearance name the csv file is looking like below.
ACCESS.CSV:
Name Clearance name card number
4th Floor, IT iT 1234
Graham, John Temps 23489
lori, Julie Access (if no card number it is placing with next record details)
Badge 56892 56892 (if clearance name is empty then card number Is saving into clearance name field in csv file.)
The text file data is line by line .
How to read and save it to csv file
What I have tried:
Tried many things but failed.
searched in google but didn't find proper answer.
Please help me.
Thanks inadvance.