close
透過公司ERP的SOAP API取得XML格式同仁資料,同步到client端
解析XML範例如下:
//開始執行更新員工資料作業 if (!String.IsNullOrEmpty(url) && !String.IsNullOrEmpty(nb)) { post.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>"); post.Append("<SOAP-ENV:Body>"); post.Append("<getCheckUser>"); post.AppendFormat("<Hostname>{0}</Hostname>", nb); post.Append("</getCheckUser>"); post.Append("</SOAP-ENV:Body>"); post.Append("</SOAP-ENV:Envelope>"); using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); //這行要加才是標準格式 response = wc.UploadString(url, "POST", post.ToString()); //取得response的String XmlDocument doc = new XmlDocument(); doc.LoadXml(response);//讀取回傳的String XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); //這是SOAP 1.1 var xmlNode = doc.SelectSingleNode("//soap:Body/*", mgr); XmlDocument XmlDoc = new XmlDocument(); XmlDoc.LoadXml(xmlNode.FirstChild.InnerText); XmlNodeList NodeLists = XmlDoc.SelectNodes("DSS/empInfo"); foreach (XmlElement element in NodeLists) { empNo = element.GetElementsByTagName("empNo")[0].InnerText; deptNo = element.GetElementsByTagName("deptNo")[0].InnerText; postName = element.GetElementsByTagName("postName")[0].InnerText; chiName = element.GetElementsByTagName("chiName")[0].InnerText; checkCode = element.GetElementsByTagName("checkCode")[0].InnerText; sql.Clear(); sql.AppendFormat("INSERT INTO 員工資料(內碼,職工編號,姓名,單位,職稱) VALUES (N'{0}',N'{1}',N'{2}',N'{3}',N'{4}')", checkCode, empNo, chiName, deptNo, postName); DB.DBInsertUpdateDelete(sql.ToString()); } } MessageBox.Show("員工資料同步完成!"); }
全站熱搜
留言列表