web Service 接口对xml数据处理
Posted 团子先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web Service 接口对xml数据处理相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Xml; using IM.BLL; using IM.Model; using System.IO; using System.Text; using WebSite.Public.FlashUpload; namespace WebSite.Api.Emp { /// <summary> /// EmpWebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class EmpWebService : System.Web.Services.WebService { private const string USERNAME = "sucgm"; private const string PWD = "[email protected]#"; #region 插入人员数据 [WebMethod] public string synchEmployee(string uName, string pwd, string xml) { tb_EmployeeBLL ol = new tb_EmployeeBLL(); string result = "0"; //发送成功 if (!uName.Equals(USERNAME) && !pwd.Equals(PWD)) return "-1"; try { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); foreach (XmlNode node in doc.SelectNodes("employees/employee")) { tb_EmployeeModel mod = new tb_EmployeeModel(); mod.sysId = node["sysId"].InnerText; mod.name = node["name"].InnerText; mod.createDate = Convert.ToDateTime(node["createDate"].InnerText.Length > 0 ? node["createDate"].InnerText : "1900-01-01 00:00:00"); mod.sex = node["sex"].InnerText; mod.mobile = node["mobile"].InnerText; mod.identityNo = node["identityNo"].InnerText; mod.province = node["province"].InnerText; mod.birthDate = node["birthDate"].InnerText; mod.nation = node["nation"].InnerText; mod.blood = node["blood"].InnerText; mod.Subcontractor = node["subcontractor"].InnerText; mod.Worktype = node["worktype"].InnerText; mod.shift = node["shift"].InnerText; mod.empType = node["empType"].InnerText; mod.entryTime = node["entryTime"].InnerText; mod.outTime = node["outTime"].InnerText; mod.medicalCertificate = node["medicalCertificate"].InnerText; mod.Contract = node["contract"].InnerText; mod.contractTime = node["contractTime"].InnerText; mod.Certificate = node["certificate"].InnerText; mod.certificateName = node["certificateName"].InnerText; mod.townSecure = node["townSecure"].InnerText; mod.otherSecure = node["otherSecure"].InnerText; mod.safeCard = node["safeCard"].InnerText; mod.cardTime = node["cardTime"].InnerText; mod.safePost = node["safePost"].InnerText; mod.postTime = node["postTime"].InnerText; mod.orderBook = node["orderBook"].InnerText; mod.orderTime = node["orderTime"].InnerText; mod.safeEducationJiaoDi = node["safeEducationJiaoDi"].InnerText; mod.peccancys = node["peccancys"].InnerText; mod.safetys = node["safetys"].InnerText; mod.projectCode = node["projectCode"].InnerText; mod.createUserMsgCode = node["createUserMsgCode"].InnerText; mod.createUserName = node["createUserName"].InnerText; string c = node["picstr"].InnerText; mod.fileName = node["fileName"].InnerText; mod.del = node["del"].InnerText; if (c.Length > 0) { byte[] a = Convert.FromBase64String(c); ///empphoto/项目编号/xxxx.jpg string paths = Server.MapPath("../../") + "Upload\\" + "\\empphoto\\" + mod.projectCode; if (!Directory.Exists(paths)) { Directory.CreateDirectory(paths); } string path = paths + "\\" + mod.fileName + ""; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); fs.Write(a, 0, a.Length); if (fs != null) { fs.Close(); } mod.picstr = "Upload/empphoto/" + mod.projectCode + "/" + mod.fileName + ""; } else { mod.picstr = "Upload/empphoto/" + "NoPhoto.jpg"; } ol.Add(mod); } } catch (Exception e) { result = e.Message; //返回异常信息 } return result; } #endregion #region 插入门禁信息 [WebMethod] public string synchCardRecord(string uName, string pwd, string xml) { tb_Emp_SysnBLL ol = new tb_Emp_SysnBLL(); string result = "0"; //发送成功 if (!uName.Equals(USERNAME) && !pwd.Equals(PWD)) return "-1"; try { XmlDocument doc = new XmlDocument(); string sql1 = ""; doc.LoadXml(xml); foreach (XmlNode node in doc.SelectNodes("cardrecords/cardrecord")) { tb_Emp_Card_RecordModel mod = new tb_Emp_Card_RecordModel(); mod.recordId = Convert.ToInt32(node["recordId"].InnerText); mod.empId = node["empId"].InnerText; mod.cardNo = Convert.ToDecimal(node["cardNo"].InnerText); mod.epcCardNo = node["epcCardNo"].InnerText; mod.deviceNo = Convert.ToInt32(node["deviceNo"].InnerText); mod.readerNo = Convert.ToInt32(node["readerNo"].InnerText); mod.status = Convert.ToInt32(node["status"].InnerText); mod.ioDate = Convert.ToDateTime(node["ioDate"].InnerText.Length > 0 ? node["ioDate"].InnerText : "1900-01-01 00:00:00"); mod.analysis = Convert.ToInt32(node["analysis"].InnerText); mod.createDate = Convert.ToDateTime(node["createDate"].InnerText.Length > 0 ? node["createDate"].InnerText : "1900-01-01 00:00:00"); mod.result = Convert.ToInt32(node["result"].InnerText); mod.projectCode = node["projectCode"].InnerText; mod.fileName = node["fileName"].InnerText; string c = node["picstr"].InnerText; if (c.Length > 0) { byte[] a = Convert.FromBase64String(c); ///empphoto/项目编号/xxxx.jpg string paths = Server.MapPath("../../") + "Upload\\" + "\\empphoto\\" + "\\cardrecord\\" + mod.projectCode; if (!Directory.Exists(paths)) { Directory.CreateDirectory(paths); } string path = paths + "\\" + mod.fileName + ""; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); fs.Write(a, 0, a.Length); if (fs != null) { fs.Close(); } mod.picstr = "Upload/empphoto/cardrecord/" + mod.projectCode + "/" + mod.fileName + ""; } else { mod.picstr = ""; } sql1 += synchCardRecordSQL(mod); //ol.Add(mod); } ol.Add(sql1); } catch (Exception e) { result = e.Message; //返回异常信息 } return result; } #endregion #region 门禁sql拼接 /// <summary> /// 接收mod数据,返回拼接的sql语句 /// </summary> /// <param name="mod"></param> /// <returns></returns> public string synchCardRecordSQL(tb_Emp_Card_RecordModel mod) { StringBuilder strSql = new StringBuilder(); strSql.Append("if not exists(select 1 from tb_Emp_Card_Record where recordId=‘" + mod.recordId + "‘ and projectCode=‘" + mod.projectCode + "‘)"); strSql.Append(" insert into tb_Emp_Card_Record ( "); strSql.Append(" recordId,empId,cardNo,epcCardNo,deviceNo,readerNo,status,ioDate,"); strSql.Append("analysis,createDate,result,projectCode,picstr,similarity,fileName "); strSql.Append(" ) "); strSql.Append(" values ("); strSql.Append(" ‘" + mod.recordId + "‘,‘" + mod.empId + "‘,‘" + mod.cardNo + "‘,‘" + mod.epcCardNo + "‘,‘" + mod.deviceNo + "‘,‘" + mod.readerNo + "‘,‘" + mod.status + "‘,‘" + mod.ioDate + "‘,"); strSql.Append("‘" + mod.analysis + "‘,‘" + mod.createDate + "‘,‘" + mod.result + "‘,‘" + mod.projectCode + "‘,‘" + mod.picstr + "‘,‘" + mod.similarity + "‘,‘" + mod.fileName + "‘ "); strSql.Append(" ) "); return strSql.ToString(); } #endregion #region 插入违章信息 [WebMethod] public string synchViolationRecord(string uName, string pwd, string xml) { tb_Emp_SysnBLL ol = new tb_Emp_SysnBLL(); string result = "0"; //发送成功 if (!uName.Equals(USERNAME) && !pwd.Equals(PWD)) return "-1"; try { XmlDocument doc = new XmlDocument(); string sql1 = ""; doc.LoadXml(xml); foreach (XmlNode node in doc.SelectNodes("violations/violation")) { tb_Emp_ViolationRecordModel mod = new tb_Emp_ViolationRecordModel(); mod.sysId = Guid.NewGuid().ToString().Replace("-", ""); mod.recordId = node["recordId"].InnerText; mod.empId = node["empId"].InnerText; mod.violationDate = node["violationDate"].InnerText; mod.grade = node["grade"].InnerText; mod.types = node["types"].InnerText; mod.description = node["description"].InnerText; mod.vtype = node["vtype"].InnerText; mod.createDate = Convert.ToDateTime(node["createDate"].InnerText.Length > 0 ? node["createDate"].InnerText : "1900-01-01 00:00:00"); mod.projectCode = node["projectCode"].InnerText; mod.createUserMsgCode = node["createUserMsgCode"].InnerText; mod.createUserName = node["createUserName"].InnerText; mod.del = node["del"].InnerText; try { foreach (XmlNode node1 in node.SelectNodes("fileName")) { string sysId = node1["sysId"].InnerText; string name = node1["name"].InnerText; string uploadName = node1["uploadName"].InnerText; string filestr = node1["filestr"].InnerText; string Uploadtype = "tb_Emp_ViolationRecord"; string Foreignkey = mod.recordId; string filetype = "application/octet-stream"; string savetype = "0"; string SaveServer = ""; string c = filestr; if (c.Length > 0) { byte[] a = Convert.FromBase64String(c); ///empphoto/项目编号/xxxx.jpg string paths = Server.MapPath("../../") + "Upload\\empphoto\\ViolationRecord\\" + mod.projectCode + "\\" + mod.sysId; if (!Directory.Exists(paths)) { Directory.CreateDirectory(paths); } string path = paths + "\\" + uploadName + ""; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); fs.Write(a, 0, a.Length); if (fs != null) { fs.Close(); } FileInfo fileInfo = new FileInfo(path); string filesize = fileInfo.Length.ToString(); //string filesize = "0"; //string FilePath = fileInfo.DirectoryName; string FilePath = paths; string FileAppPath = "Upload\\empphoto\\ViolationRecord\\" + mod.projectCode + "\\" + mod.sysId + "\\" + uploadName + ""; string filenewname = uploadName; string CreateUserId = ""; string createdate = DateTime.Now.ToString(); string downloads = "0"; StringBuilder strSql2 = new StringBuilder(); strSql2.Append("if not exists(select 1 from tb_UploadFileList where SysId=‘" + sysId + "‘)"); strSql2.Append(" insert into tb_UploadFileList ( "); strSql2.Append(" SysId,UploadType,ForeignKey,UploadName,FileType,FileSize,SaveType,SaveServer,FilePath,"); strSql2.Append("FileAppPath,FileNewName,CreateUserId,CreateDate,Downloads "); strSql2.Append(" ) "); strSql2.Append(" values ("); //strSql2.Append(" "+string.Format("‘{0}‘,",mod.sysId)+""); strSql2.Append(" ‘" + sysId + "‘,‘" + Uploadtype + "‘,‘" + Foreignkey + "‘,‘" + name + "‘,‘" + filetype + "‘," + filesize + ",‘" + savetype + "‘,‘" + SaveServer + "‘,‘" + FilePath + "‘,"); strSql2.Append("‘" + FileAppPath + "‘,‘" + filenewname + "‘,‘" + CreateUserId + "‘,‘" + createdate + "‘," + downloads + " "); strSql2.Append(" ) "); //strSql2.Append(" else update tb_Emp_ViolationRecord "); //strSql2.Append(" set "); //strSql2.Append(" empId=‘" + mod.empId + "‘,violationDate=‘" + mod.violationDate + "‘,grade=‘" + mod.grade + "‘,types=‘" + mod.types + "‘,description=‘" + mod.description + "‘, "); //strSql2.Append(" vtype=‘" + mod.vtype + "‘,createDate=‘" + mod.createDate + "‘,del=‘" + mod.del + "‘ where recordId=‘" + mod.recordId + "‘ and projectCode=‘" + mod.projectCode + "‘) "); ol.Addfj(strSql2.ToString()); } } } catch { } sql1 += synchViolationRecordSQL(mod); } ol.Add(sql1); } catch (Exception e) { result = e.Message; //返回异常信息 } return result; } #endregion #region 违章sql拼接 public string synchViolationRecordSQL(tb_Emp_ViolationRecordModel mod) { StringBuilder strSql = new StringBuilder(); strSql.Append("if not exists(select 1 from tb_Emp_ViolationRecord where recordId=‘" + mod.recordId + "‘ and projectCode=‘" + mod.projectCode + "‘)"); strSql.Append(" insert into tb_Emp_ViolationRecord ( "); strSql.Append(" sysId,recordId,empId,violationDate,grade,types,description,vtype,createDate,"); strSql.Append("projectCode,createUserMsgCode,createUserName,del "); strSql.Append(" ) "); strSql.Append(" values ("); strSql.Append(" ‘" + mod.sysId + "‘,‘" + mod.recordId + "‘,‘" + mod.empId + "‘,‘" + mod.violationDate + "‘,‘" + mod.grade + "‘,‘" + mod.types + "‘,‘" + mod.description + "‘,‘" + mod.vtype + "‘,‘" + mod.createDate + "‘,"); strSql.Append("‘" + mod.projectCode + "‘,‘" + mod.createUserMsgCode + "‘,‘" + mod.createUserName + "‘,‘" + mod.del + "‘ "); strSql.Append(" ) else "); strSql.Append(" update tb_Emp_ViolationRecord "); strSql.Append(" set "); strSql.Append(" empId=‘" + mod.empId + "‘,violationDate=‘" + mod.violationDate + "‘,grade=‘" + mod.grade + "‘,types=‘" + mod.types + "‘,description=‘" + mod.description + "‘, "); strSql.Append(" vtype=‘" + mod.vtype + "‘,createDate=‘" + mod.createDate + "‘,createUserMsgCode=‘" + mod.createUserMsgCode + "‘,createUserName=‘" + mod.createUserName + "‘,del=‘" + mod.del + "‘ where recordId=‘" + mod.recordId + "‘ and projectCode=‘" + mod.projectCode + "‘ "); return strSql.ToString(); } #endregion #region 插入人员定位读头信息 [WebMethod] public string synchReaders(string uName, string pwd, string xml) { tb_Emp_SysnBLL ol = new tb_Emp_SysnBLL(); string result = "0"; //发送成功 if (!uName.Equals(USERNAME) && !pwd.Equals(PWD)) return "-1"; try { XmlDocument doc = new XmlDocument(); string sql1 = ""; doc.LoadXml(xml); foreach (XmlNode node in doc.SelectNodes("readers/reader")) { tb_Emp_ReadersModel mod = new tb_Emp_ReadersModel(); mod.sysId = Guid.NewGuid().ToString().Replace("-", ""); mod.recordId = Convert.ToInt32(node["recordId"].InnerText); mod.lrmID = Convert.ToInt32(node["lrmID"].InnerText); mod.lrmName = node["lrmName"].InnerText; mod.lrmType = Convert.ToInt32(node["lrmType"].InnerText); mod.lrmX = Convert.ToInt32(node["lrmX"].InnerText); mod.lrmY = Convert.ToInt32(node["lrmY"].InnerText); mod.lrmState = Convert.ToInt32(node["lrmState"].InnerText); if (node["lrmTime"].InnerText.Length > 0) mod.lrmTime=Convert.ToDateTime(node["lrmTime"].InnerText); else mod.lrmTime = null; mod.rmk = node["rmk"].InnerText; mod.monitorDaySecurity = node["monitorDaySecurity"].InnerText; mod.commentMans = node["commentMans"].InnerText; mod.dangerousMans = node["dangerousMans"].InnerText; mod.mapId = node["mapId"].InnerText; mod.lrmRadius = Convert.ToInt32(node["lrmRadius"].InnerText); //mod.createDate = Convert.ToDateTime(node["createDate"].InnerText.Length > 0 ? node["createDate"].InnerText : DBNull.Value.ToString()); if (node["createDate"].InnerText.Length > 0) mod.createDate=Convert.ToDateTime(node["createDate"].InnerText); else mod.createDate = null; mod.projectCode = node["projectCode"].InnerText; mod.del = node["del"].InnerText; sql1 += synchReadersSQL(mod); //ol.Add(mod); } ol.Add(sql1); } catch (Exception e) { result = e.Message; //返回异常信息 } return result; } #endregion #region 人员定位读头sql拼接 public string synchReadersSQL(tb_Emp_ReadersModel mod) { StringBuilder strSql = new StringBuilder(); strSql.Append("if not exists(select 1 from tb_Emp_Readers where recordId=‘" + mod.recordId + 以上是关于web Service 接口对xml数据处理的主要内容,如果未能解决你的问题,请参考以下文章