nazima

nazima

  • NA
  • 6
  • 0

How to parse a text file in C#.Net

Sep 12 2011 2:51 AM
Hi..i want to know how can i parse a text file in Csharp.Net .I am giving an example of my textfile.This is the format of my textfile:

  CompanyName
DepartMentNo:1234
Address:

Date     EmpID  EmpName  Address  Department  Position
08/08  001  Nazima  address of emp  Developer  Fresher
08/08  002   Vali  address of emp  Developer  Fresher
08/08  003   Farah  address of emp  DBAdmin  Fresher

Now i have to go thru each and every line i have to find the departmentNo and then it has to be inserted in SQL DB..Then i have to go thru the records and insert this records into SQL DB..

i have written a code for searching DepatmentNo but once the control comes to if condition it does not perform any operation and the control goes to next statement..May i know how can i retrieve those data and store it in database.

This is my code:

StreamReader sr = new StreamReader(file);
  string line = null;
  line = sr.ReadToEnd();
  str = line.Trim().Split('\n');
foreach (string item in str)
  {
  if (item.IndexOf("DepartmentNo")> -1)
  {
  string DNo1 = item.Substring(15, 23);
  DNo = Convert.ToInt32(DNo1);
  }
  }

Hope u understood my doubt.And please do guide me..

Answers (3)