public void ProcessRequest(HttpContext context) { context.Response.Clear(); string username = context.Request.QueryString["username"];//操作人 HttpFileCollection postedFile = context.Request.Files; //判断是否有数据 if (postedFile != null && postedFile.Count > 0) { int length = postedFile[0].ContentLength;//文件大小 string filename = System.IO.Path.GetFileNameWithoutExtension(postedFile[0].FileName);//文件名,不包括扩展名 string path = HttpContext.Current.Server.MapPath("/XmlFiles/");//文件存放路径 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path);//创建文件夹 string allfile = path + filename + DateTime.Now.ToString("yyyy-MM-dd") + ".xml";//文件路径+文件名 postedFile[0].SaveAs(allfile);//保存文件内容 if (length > 0 && length < 512000)//文件最大允许500k { string type = System.IO.Path.GetExtension(postedFile[0].FileName);//文件类型 if (type == ".xml") { //读取文件 QzSpecialisticDAL dal = new QzSpecialisticDAL(); QZ_IMPORT_LOG log = new QZ_IMPORT_LOG(); try { XmlDocument xmlDoc = new XmlDocument(); //加载xml xmlDoc.Load(allfile); //path+filename为文件路径 //读取指定的节点 #region 1.配送台账:读取QZ_DISPATCH_DETAILS节点 QZ_DISPATCH_DETAILS details = dal.GetModels<QZ_DISPATCH_DETAILS>(xmlDoc,"/QZ_SPECIALISTIC/QZ_DISPATCH_DETAILS"); dal.InsertXmlData(details.DetailList,username,out log); log.IMPORT_NAME = "配送台账";//导入界面 if (!dal.AddImportLog(log)) { string res1 = "{\"status\" : \"success\",\"msg\": \"配送台账日志插入错误上传失败!\"}"; context.Response.Write(res1); return; } #endregion #region 2.零售台账:读取QZ_SPECIALISTIC_SALEDETAILS节点 QZ_SPECIALISTIC_SALEDETAILS sales = dal.GetModels<QZ_SPECIALISTIC_SALEDETAILS>(xmlDoc,"/QZ_SPECIALISTIC/QZ_SPECIALISTIC_SALEDETAILS"); dal.InsertXmlData(sales.DetailList,out log); log.IMPORT_NAME = "零售台账";//导入界面 if (!dal.AddImportLog(log)) { string res1 = "{\"status\" : \"success\",\"msg\": \"零售台账日志插入错误上传失败!\"}"; context.Response.Write(res1); return; } #endregion string res = "{\"status\" : \"success\",\"msg\": \"上传成功!\"}"; context.Response.Write(res); } catch (Exception e) { string res = "{\"status\" : \"error\",\"msg\": \"文件格式错误上传失败!\"}"; context.Response.Write(res); return; } } else { string res = "{\"status\" : \"error\",\"msg\": \"只允许上传.xml类型的文件!\"}"; context.Response.Write(res); return; } } else { string res = "{\"status\" : \"error\",\"msg\": \"文件过大!\"}"; context.Response.Write(res); return; } } }
2.所调用方法:
InsertXmlData方法,实现入库校验操作:
public void InsertXmlData<T>(List<T> models,string username,out QZ_IMPORT_LOG logmodel) where T : new() { QZ_IMPORT_LOG log =new QZ_IMPORT_LOG(); int sum = models.Count; int j = 0; string mismessage = ""; string[] s = models.ToString().Split(new char[] { '.' },StringSplitOptions.RemoveEmptyEntries); string model = s[s.Length - 1].Replace("]","").Trim(); if (models != null)//判断节点下是否有子节点 { //循环遍历detail结点下的子结点 foreach (T submodel in models) { //校验Id是否重复 string id = submodel.GetType().GetProperty("ID").GetValue(submodel,null).ToString(); if (ExixtsID(model,id)) { if (OracleHelper.Insert<T>(submodel))//插入数据库 { j++;//成功上传条数 } else { //上传失败 mismessage += "id=" + id + "数据插入错误,上传失败;"; } } else { //上传失败 mismessage += "id=" + id + "已导入;"; } } //录入上传日志数据 log.IMPORT_DATE = DateTime.Now;//导入时间 log.IMPORT_TABLENAME = model;//导入表名 log.IMPORT_NUM = sum;//导入总条数 log.IMPORT_NUM_OK = j;//导入成功条数 log.REMARK = mismessage;//未成功错误信息 log.OPERATOR = username;//操作人 } logmodel = log; }
GetModels方法获取反序列化数据:
public T GetModels<T>(XmlDocument xmlDoc,string singleNode) where T : new() { XmlNode xmlNode = xmlDoc.SelectSingleNode(singleNode); StringReader stream = new StringReader(xmlNode.OuterXml); string xml = stream.ReadToEnd(); return XmlDeserialize<T>(xml,Encoding.UTF8); }XmlDeserialize实现反序列化:
public T XmlDeserialize<T>(string s,Encoding encoding) where T : new() { if (string.IsNullOrEmpty(s)) throw new ArgumentNullException("s"); if (encoding == null) throw new ArgumentNullException("encoding"); XmlSerializer mySerializer = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) { using (StreamReader sr = new StreamReader(ms,encoding)) { return (T)mySerializer.Deserialize(sr); } } }
xml文件格式:
<QZ_SPECIALISTIC><QZ_SPECIALISTIC_DESTROYS> <QZ_SPECIALISTIC_DESTROY> <ID>4</ID> <DESTROY_DATE>2016-01-11</DESTROY_DATE> <SPECIALISTIC_ID>425002</SPECIALISTIC_ID> <SPECIALISTIC_NAME>珍新泉港店</SPECIALISTIC_NAME> <MEAT_CODE>101</MEAT_CODE> <MEAT_NAME>猪舌</MEAT_NAME> <WEIGHT>28</WEIGHT> <OPERATOR>阿玉姐姐</OPERATOR> <UPLOAD_TYPE>2</UPLOAD_TYPE> </QZ_SPECIALISTIC_DESTROY> <QZ_SPECIALISTIC_DESTROY> <ID>5</ID> <DESTROY_DATE>2016-01-11</DESTROY_DATE> <SPECIALISTIC_ID>425002</SPECIALISTIC_ID> <SPECIALISTIC_NAME>珍新泉港店</SPECIALISTIC_NAME> <MEAT_CODE>102</MEAT_CODE> <MEAT_NAME>猪条</MEAT_NAME> <WEIGHT>t</WEIGHT> <OPERATOR>阿玉姐姐</OPERATOR> <UPLOAD_TYPE>2</UPLOAD_TYPE> </QZ_SPECIALISTIC_DESTROY> </QZ_SPECIALISTIC_DESTROYS> <QZ_SPECIALISTIC_SURPLUSS> <QZ_SPECIALISTIC_SURPLUS> <ID>1</ID> <REGISTER_DATE>2016-01-11</REGISTER_DATE> <SPECIALISTIC_ID>425002</SPECIALISTIC_ID> <SPECIALISTIC_NAME>珍新泉港店</SPECIALISTIC_NAME> <span style="white-space:pre"> </span><MEAT_CODE>102</MEAT_CODE> <MEAT_NAME>猪条</MEAT_NAME> <WEIGHT>150</WEIGHT> <OPERATOR>吴老大</OPERATOR> <span style="white-space:pre"> </span><UPLOAD_TYPE>2</UPLOAD_TYPE> </QZ_SPECIALISTIC_SURPLUS> <QZ_SPECIALISTIC_SURPLUS> <ID>2</ID> <REGISTER_DATE>2016-01-11</REGISTER_DATE> <SPECIALISTIC_ID>425002</SPECIALISTIC_ID> <SPECIALISTIC_NAME>珍新泉港店</SPECIALISTIC_NAME> <span style="white-space:pre"> </span><MEAT_CODE>102</MEAT_CODE> <MEAT_NAME>猪条</MEAT_NAME> <WEIGHT>150</WEIGHT> <OPERATOR>吴老大</OPERATOR> <span style="white-space:pre"> </span><UPLOAD_TYPE>2</UPLOAD_TYPE> </QZ_SPECIALISTIC_SURPLUS> </QZ_SPECIALISTIC_SURPLUSS></QZ_SPECIALISTIC>