(5)XML转化成TXT
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace test1 { public class B2eGetXML { //先以生成的发送XML为例,(XMLRequest.xml) public List<b2e0035_rq> GetXMLToList() { List<b2e0035_rq> list = new List<b2e0035_rq>(); XmlDocument doc = new XmlDocument(); b2e0035_rq b2e0035_rq_tem = new b2e0035_rq(); //加载XML文件 doc.Load("C:/Users/zuomeiyan/Desktop/XMLRequest.xml"); //获取b2e0035-rq节点 XmlNodeList xnl_b2e0035_rq = doc.SelectNodes("bocb2e/trans/trn-b2e0035-rq/b2e0035-rq"); foreach (XmlNode xn_b2e0035_rq in xnl_b2e0035_rq) { XmlElement xe_b2e0035_rq = (XmlElement)xn_b2e0035_rq; b2e0035_rq_tem.ibknum = xe_b2e0035_rq.SelectSingleNode("ibknum").InnerText; b2e0035_rq_tem.actacn = xe_b2e0035_rq.SelectSingleNode("actacn").InnerText; b2e0035_rq_tem.type = xe_b2e0035_rq.SelectSingleNode("type").InnerText; XmlNode xn_from = xe_b2e0035_rq.SelectSingleNode("datescope/from"); //string转换成DateTime型 b2e0035_rq_tem.datescope_from=DateTime.ParseExact(xn_from.InnerText, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces); XmlNode xn_to = xe_b2e0035_rq.SelectSingleNode("datescope/to"); //string转换成DateTime型 b2e0035_rq_tem.datescope_to = DateTime.ParseExact(xn_to.InnerText, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces); b2e0035_rq_tem.amountscope_from = decimal.Parse(xe_b2e0035_rq.SelectSingleNode("amountscope/from").InnerText); b2e0035_rq_tem.amountscope_to = decimal.Parse(xe_b2e0035_rq.SelectSingleNode("amountscope/to").InnerText); b2e0035_rq_tem.begnum = xe_b2e0035_rq.SelectSingleNode("begnum").InnerText; b2e0035_rq_tem.recnum = xe_b2e0035_rq.SelectSingleNode("recnum").InnerText; b2e0035_rq_tem.direction = xe_b2e0035_rq.SelectSingleNode("direction").InnerText; list.Add(b2e0035_rq_tem); } return list; } public void WriteToNewTxt(List<b2e0035_rq> lst) { string savePath = @"C:\Users\zuomeiyan\Desktop\new.txt"; //将生成的新list写入文件 FileStream fsnew = new FileStream(savePath, FileMode.Create); StreamWriter sw = new StreamWriter(fsnew, Encoding.UTF8); try { sw.Flush(); for (int i = 0; i < lst.Count; i++) { sw.WriteLine(lst[i]); } } catch { } finally { //关闭此文件 sw.Flush(); sw.Close(); fsnew.Close(); } } } }