Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
3.60/5 (3 votes)
See more:
Hii frnds,

I have following .txt file,which i have to read. and convert it to datatable.
columns would be---(Type),(Category),(Grade style),(Random answers),Randomize Options,(Question),(A),(B),(C),(D),(E),(Correct) ,(Points) ,(CF) ,(WF)

row data would be after : part


...............txt file start here...............





(Type): multiplechoice
(Category): 0
(Grade style): 0
(Random answers): 0
(Question):
(A):
(B):
(C):
(D):
(E):
(F):
(G):
(H):
(I):
(J):
(Correct):
(Points)
: 1
(CF):
(WF):


(Type): multiplechoice
(Category): 0
(Grade style): 0
(Random answers)
: 0
(Question):
(A):
(B):
(C):
(D):
(E):
(F):
(G):
(H):
(I):
(J):
(Correct):
(Points): 1
(CF):
(WF):



(Type): truefalse
(Category): 0
(Question):
(A): True
(B): False
(Correct):
(Points): 1
(CF):
(WF):



(Type): freetext
(Category): 0
(Question):
(A):
(B):
(C):
(D):
(E):
(Points): 1
(CF):
(WF):

..........txt file ends here...........
Posted
Updated 13-Feb-14 22:15pm
v2
Comments
You explained the requirement not problem. Please tell us the problem with the code.
Aysha Patel 14-Feb-14 2:27am    
i dnt know how to make datatable...with my requierments

You can use below Helper function for your requirement

for this namespace system.Data Required

//Add following namespace in namespace section

using system.Data;


//below is the helper function which will take input as a path of your file and will return you datatable


C#
public DataTable ConvertToDataTable(string path)
		{
			string s = string.Empty;
			string[] stringarry=System.IO.File.ReadAllLines(path);
			System.Data.DataTable data = new System.Data.DataTable();
			data.Columns.Add("Type", typeof(string));
			data.Columns.Add("Category", typeof(string));
			data.Columns.Add("Grade style", typeof(string));
			data.Columns.Add("Random answers", typeof(string));
			data.Columns.Add("Randomize Options", typeof(string));
			data.Columns.Add("Question", typeof(string));
			data.Columns.Add("A", typeof(string));
			data.Columns.Add("B", typeof(string));
			data.Columns.Add("C", typeof(string));
			data.Columns.Add("D", typeof(string));
			data.Columns.Add("E", typeof(string));
			data.Columns.Add("Correct", typeof(string));
			data.Columns.Add("Points", typeof(string));
			data.Columns.Add("CF", typeof(string));
			data.Columns.Add("WF", typeof(string));
			data.AcceptChanges();

			DataRow dr=null;
			foreach (string st in stringarry)
			{ 
				 if(!string.IsNullOrEmpty(st))
				 {
					string[] splitarray=st.Split(':');

					if (splitarray != null && splitarray.Length > 1)
					{
						if (splitarray[0].ToUpper() == "(Type)".ToUpper())
						{
							dr = data.NewRow();
							dr["Type"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Category)".ToUpper())
						{
							dr["Category"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Grade style)".ToUpper())
						{
							dr["Grade style"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Random answers)".ToUpper())
						{
							dr["Random answers"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Randomize Options)".ToUpper())
						{
							dr["Randomize Options"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Question)".ToUpper())
						{
							dr["Question"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(A)".ToUpper())
						{
							dr["A"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(B)".ToUpper())
						{
							dr["B"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(C)".ToUpper())
						{
							dr["C"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(D)".ToUpper())
						{
							dr["D"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(E)".ToUpper())
						{
							dr["E"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Correct)".ToUpper())
						{
							dr["Correct"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(Points)".ToUpper())
						{
							dr["Points"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(CF)".ToUpper())
						{
							dr["Correct"] = splitarray[1];
						}
						else if (splitarray[0].ToUpper() == "(WF)".ToUpper())
						{
							dr["WF"] = splitarray[1];
							data.Rows.Add(dr);
						}
						
					}
				 }
			}
			data.AcceptChanges();
			return data;
		}



:) Happy Coding.


Please ask any query if you have in above code.


Please mark it as solution if it helps you.
 
Share this answer
 
v2
Comments
Aysha Patel 1-Apr-14 7:32am    
Thanks Ashok....
please specified your requirement properly. its seems confusing.
 
Share this answer
 
Comments
Aysha Patel 14-Feb-14 4:18am    
how to convert txt file to datatable..as shown in my text file above i want columns(characters before ":")and rows(characters after ':")
See my response to the duplicate of this question.
 
Share this answer
 

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