Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,
I have a text file and need to read from line number 4 to end(line no 100) and convert it into C# Class CountryList(Properties).
I need to Make from Line number 4 to end, split comma(,) separate values and assign to C# Class CountryList(Properties)

Sample Text File :-
   Country : INDIA    // Line No : 1
Currency : INR

1,RRRR
2,EEEE
3,WWWW
4,WWAA
5,FDSDF
6,WERWER
7,SFSDF
8,FDAF
9,DFDSFA
..... SO ON


What I have tried:

C# Code:-
C#
public class CountryList
{
    public string ID { get; set; }
    public string NAME { get; set; }
}

string strReadFile = @"D:\Country.txt";
    int startLine = 4;
    int lineCount = 100;
    var fileLines = File.ReadAllLines(strReadFile)
                        .Skip((startLine))
                        .Take(lineCount)
                        .ToList();
Posted
Updated 10-Jun-16 6:57am
v2
Comments
BillWoodruff 10-Jun-16 17:15pm    
I think the three answers here probably meet your needs. You might consider doing something with csv to xml:

https://www.csvreader.com/code/cs/csv_to_xml.php

once you have xml, then use Linq to XML ?

try this

C#
var fileLines = File.ReadAllLines(strReadFile).Skip((startLine)).Take(lineCount).ToList();
       List<CountryList> lst = new List<CountryList>();
       foreach (string line in fileLines)
       {
           string[] temp = line.Split(',');
           if (temp.Length >= 2)
           {
               CountryList country = new CountryList();
               country.ID = temp[0].Trim();
               country.NAME = temp[1].Trim();
               lst.Add(country);
           }
       }
 
Share this answer
 
One of the common problems with "quick and easy" solutions with regards to comma delimited files is the fact that eventually you will run across a csv that has double quote text qualifiers. So for this end, I always recommend TextFieldParser.

C#
using Microsoft.VisualBasic.FileIO;

void Main()
{
	string strReadFile = @"D:\Country.txt";
	List<CountryList> lst = ParseCountryList(strReadFile, ",", true);
}

class CountryList
{
	public string ID { get; set; }
	public string NAME { get; set; }
}

static List<CountryList> ParseCountryList(string FileName, string Delimiter, bool IsQualified)
{
	var result = new List<CountryList>();
	using (var tfp = new TextFieldParser(FileName) {
		Delimiters = new string[] { Delimiter },
		HasFieldsEnclosedInQuotes = IsQualified
	})
	{
		while (!tfp.EndOfData)
		{
			var fields = tfp.ReadFields();
			if (fields.Length >= 2)
			{
				var cl = new CountryList
				{
					ID = fields[0].Trim(),
					NAME = fields[1].Trim()
				};
				result.Add(cl);
			}
		}
	}
	return result;
}
 
Share this answer
 
AFell raises good points about CSV file potential issues.
I'm going to assume that for now you know that the file format never has an ID value that's quoted including a comma.
At a minimum this should give you a starting point for something more sophisticated if necessary.
C#
// You don't need to read all of the lines into an array, just read 'em as you go
char [] delimiters = new char[] { ',' };
var fileLines = File.ReadLines(strReadFile)
                    .Skip(startLine)
                    // .Take(..) is necessary ONLY if you DON'T want to read to the end
                    // in your question you said "from Line number 4 to end"
                    // .Take(lineCount)
                    .Select(line => {
                       string[] ss = line.Split(delimiters, 2);
                       return (ss.Length == 2)
                              ? new CountryList() { ID = ss[0], NAME = ss[1] }
                              : null;              // null for ill-formatted line
                     })
                    .Where(x => x != null)      // filter to good lines
                    .ToList();

(I didn't test this... There may be a typo or two...)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900