如何读取.txt文件并将其转换为datatable? [英] How to read .txt file and convert it to datatable?

查看:294
本文介绍了如何读取.txt文件并将其转换为datatable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii frnds,



我有以下.txt文件,我必须阅读。并将其转换为数据表。

列将是---(类型),(类别),(等级样式),(随机答案),随机选项,(问题),(A),( B),(C),(D),(E),(正确),(点),(CF),(WF)



行数据将是之后:部分





............... txt文件从这里开始.... ...........











(类型):multiplechoice

(类别):0

(等级样式):0

(随机答案):0

(问题):

(A):

(B):

( C):

(D):

(E):

(F):

(G) :

(H):

(I):

(J):

(正确):< br $>
(积分)

:1

(CF):

(WF):





(类型):multiplechoice

(类别):0

(等级样式):0

(随机答案)

:0

(问题):

(A):

(B):

(C):

(D):

(E):

(F):

(G):

(H):

(I):

( J):

(正确):

(积分):1

(CF):

(WF) ):







(类型):truefalse

(类别) :0

(问题):

(A):正确

(B):错误

(正确) ):

(积分):1

(CF):

(WF):







(类型):freetext

(类别):0

(问题) :

(A):

(B):

(C):

(D):

(E):

(积分):1

(CF):

(WF):



.......... txt文件在这里结束...........

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...........

推荐答案

您可以根据需要使用以下助手功能



这个命名空间system.Data必需



//在命名空间部分添加以下命名空间



使用system.Data;





//下面是帮助函数,它将输入作为文件的路径和会给你返回数据表





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


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.


请正确指定您的要求。它似乎令人困惑。
please specified your requirement properly. its seems confusing.


请参阅我对此问题副本的回复。
See my response to the duplicate of this question.


这篇关于如何读取.txt文件并将其转换为datatable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆