(3)TXT转为XML
<?xml version="1.0" encoding="utf-8"?> <bocb2e> <head /> <trans> <trn-b2e0035-rq> <b2e0035-rq> <ibknum>11111</ibknum> <actacn>111</actacn> <type>2001</type> <datescope> <from>20150320</from> <to>20150320</to> </datescope> <amountscope> <from>0</from> <to>100</to> </amountscope> <begnum /> <recnum /> <direction>0</direction> </b2e0035-rq> </trn-b2e0035-rq> </trans> </bocb2e>
该段是一个XML代码,现在我们要做的是将以下的TXT文件转化为上述格式,TXT格式如下:
ibknum|actacnc|type |datescope_from|datescope_to|direction
1111 |111 |2002|20020214 |20020214 |0
首先,有两个路径,分别是读取路径和保存路径,downfolder和savefolder。我们的txt是保存在ftp服务器上的,这一点我们下一篇文章会讲述,怎么从ftp上下载一个文件。
现在我们直接从savefolder开始,也就是已经下载好的文件夹。
下面的方法现将txt转化为一个datatable,然后转换为list
private DataTable B2eRqTxtToDt(string txtpath,string strErrMsg="",string encodingCode = "GB2312", string strSplit = "|") {if(txtpath.IndexOf("ftp")>=0){ //创建ftp请求信息 var ftpRequest = new FtpClient(ftpUser, ftpPassword); string savePath = GetDataFileSavePath(); ftpRequest.DownloadFile(savePath, txtpath, true); } //read first line StreamReader objReader = new StreamReader(savePath, Encoding.GetEncoding(encodingCode)); string sLine = objReader.ReadLine(); objReader.Close(); //all column's header string[] ColumnHeaders = sLine.ToString().Trim().Split(strSplit).Select(q => q.Trim()).ToArray(); // to datatable //直接取datatable DataTable dt = TxtService.TextFileLoader(savePath, "txt", ColumnHeaders, out strErrMsg, encodingCode, strSplit); return dt; } private List<RqList> B2eRqDtToLs(DataTable dt) { var date = DateTime.Now.AddDays(-1).Date; List<RqList> rqlist = new List<RqList>(); for (int i = 0; i < dt.Rows.Count; i++) { RqList rqls = new RqList(); b2e0035_rq b2erq = new b2e0035_rq(); b2erq.ibknum = dt.Rows[i][0].ToString(); b2erq.actacn = dt.Rows[i][1].ToString(); b2erq.type = "2002"; b2erq.datescope_from = date.Date; b2erq.datescope_to = date.Date; b2erq.amountscope_from = null; b2erq.amountscope_to = null; b2erq.begnum = null; b2erq.recnum = null; b2erq.direction = "1"; #region //这几句是读取txt文件中的.另外还有string转换为DateTime类型的。 b2erq.type = dt.Rows[i][2].ToString(); b2erq.datescope_from = DateTime.Parse(dt.Rows[i][3].ToString()); #endregion rqls.head = null; rqls.b2e0035_rq = b2erq; rqlist.Add(rqls); } return rqlist; }
将txt转化为list之后,借助写XML方法可以将其写入XML中。
public void WriteToXML(string ibknum,string actacn,string path) { //string path = @"C:\Users\zuomeiyan\XMLRequest.xml"; XmlTextWriter xtw = new XmlTextWriter(path, UTF8Encoding.UTF8); var date = DateTime.Now.AddDays(-1).Date; try { xtw.Formatting = Formatting.Indented; xtw.WriteStartDocument(); xtw.WriteStartElement("bocb2e"); ////head xtw.WriteStartElement("head"); xtw.WriteEndElement(); //交易数据块 xtw.WriteStartElement("trans"); xtw.WriteStartElement("trn-b2e0035-rq"); xtw.WriteStartElement("b2e0035-rq"); xtw.WriteElementString("ibknum", ibknum); xtw.WriteElementString("actacn", actacn); xtw.WriteElementString("type", "2002"); xtw.WriteStartElement("datescope"); xtw.WriteElementString("from", date.Date.ToString("yyyyMMdd")); xtw.WriteElementString("to", date.Date.ToString("yyyyMMdd")); xtw.WriteEndElement(); xtw.WriteStartElement("amountscope"); xtw.WriteElementString("from", null); xtw.WriteElementString("to", null); xtw.WriteEndElement(); xtw.WriteElementString("begnum", null); xtw.WriteElementString("recnum", null); xtw.WriteElementString("direction", "1"); xtw.WriteEndElement(); xtw.WriteEndElement(); xtw.WriteEndElement(); xtw.WriteEndElement(); xtw.WriteEndDocument(); } catch { } finally { xtw.Flush(); xtw.Close(); xtw.Dispose(); } }
这样我们就完成了将一个TXT文件转化为XML格式。