none
How to read data from text,csv,xml files using C# RRS feed

Answers

  • XML=>

    var  Info = XDocument.Load(completeFilePath); var descendants = employeeInfo.Descendants("Element"); var list = new List<Employee>(); foreach (var descendant in descendants) { var emp = new Employee(); emp.EmployeeId= descendant.Element("Id").Value; emp.Name = descendant.Element("Name").Value; list.Add(emp);

    }

     

    Text=>

    var fileInLines = File.ReadAllLines(completeFilePath); foreach (var line in fileInLines) { var fileAttributes = line.Split(' '); var emp = new Employee(); emp.Id = fileAttributes[0]; emp.Name = fileAttributes[1]; list.Add(emp); }

     

    CSV=>

    StreamWriter cfile = new StreamWriter(filepath);  using (var wc = cfile) { var sb = new StringBuilder(); foreach (var emp in employees) { sb.Append(emp.Id + ","); sb.Append(emp.Name + ","); } wc.WriteLine(sb.ToString()); }

    Wednesday, July 16, 2014 8:02 AM
  • hi palak04,

    can read a text file line by line and

    here textline stores the text in a line you can define a array of strings to store these line and use them

    sharing a code box with you, hope you will understand this very clear,

    C# code to read a text file line by line

    here i am assigning text to textline and displaying it in my textbox.


    Hemant Kaushik


    Wednesday, July 16, 2014 8:58 AM

All replies

  • There are lots of methods of reading each type of file you have requested.  It is hard to give one answer with a better understanding of what you are trying to accomplish. You sound like a beginner and what I usually recommend is to break your project up into pieces and tackle one piece at a time.

    jdweng

    Wednesday, July 16, 2014 7:57 AM
  • XML=>

    var  Info = XDocument.Load(completeFilePath); var descendants = employeeInfo.Descendants("Element"); var list = new List<Employee>(); foreach (var descendant in descendants) { var emp = new Employee(); emp.EmployeeId= descendant.Element("Id").Value; emp.Name = descendant.Element("Name").Value; list.Add(emp);

    }

     

    Text=>

    var fileInLines = File.ReadAllLines(completeFilePath); foreach (var line in fileInLines) { var fileAttributes = line.Split(' '); var emp = new Employee(); emp.Id = fileAttributes[0]; emp.Name = fileAttributes[1]; list.Add(emp); }

     

    CSV=>

    StreamWriter cfile = new StreamWriter(filepath);  using (var wc = cfile) { var sb = new StringBuilder(); foreach (var emp in employees) { sb.Append(emp.Id + ","); sb.Append(emp.Name + ","); } wc.WriteLine(sb.ToString()); }

    Wednesday, July 16, 2014 8:02 AM
  • hi palak04,

    can read a text file line by line and

    here textline stores the text in a line you can define a array of strings to store these line and use them

    sharing a code box with you, hope you will understand this very clear,

    C# code to read a text file line by line

    here i am assigning text to textline and displaying it in my textbox.


    Hemant Kaushik


    Wednesday, July 16, 2014 8:58 AM
  • hi ,

    You can turn to C# Programming Guide to find some more tutorails you want. If you want specific solution or suggestion on this part ,you may need to post a certain problem or error while coding.Good luck.

    __________________________________________

    kind regards

    Free C# PowerPoint component is under evaluation, any suggestions.

    Thursday, July 17, 2014 6:57 AM