Answered by:
How to read data from text,csv,xml files using C#
Question
-
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()); }- Proposed as answer by Cihan YakarMVP Thursday, July 17, 2014 7:02 AM
- Marked as answer by Herro wongMicrosoft contingent staff Thursday, July 24, 2014 9:38 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,

here i am assigning text to textline and displaying it in my textbox.
Hemant Kaushik
- Edited by Hemant Kaushik Wednesday, July 16, 2014 9:00 AM
- Marked as answer by Herro wongMicrosoft contingent staff Thursday, July 24, 2014 9:38 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
-
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()); }- Proposed as answer by Cihan YakarMVP Thursday, July 17, 2014 7:02 AM
- Marked as answer by Herro wongMicrosoft contingent staff Thursday, July 24, 2014 9:38 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,

here i am assigning text to textline and displaying it in my textbox.
Hemant Kaushik
- Edited by Hemant Kaushik Wednesday, July 16, 2014 9:00 AM
- Marked as answer by Herro wongMicrosoft contingent staff Thursday, July 24, 2014 9:38 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.