Beispiel #1
0
 public static string RestRequest(string baseUrl, string resource, string reqBody, string soapaction = "http://goodwillcis.com/JHIPLIB.SOAP.BS.HL7V3Service.HIPMessageServer", string method = "Post", string contentType = "text/xml;charset=utf-8")
 {
     try
     {
         var client = new RestClient(baseUrl);
         var req    = new RestRequest(resource, method.ToUpper() == "POST" ? Method.POST : Method.GET)
         {
             RequestFormat = DataFormat.Xml,
             XmlSerializer = new DotNetXmlSerializer()
         };
         req.RequestFormat = DataFormat.Xml;
         req.AddHeader("Content-Type", contentType);
         req.AddHeader("SOAPAction", soapaction);
         var p = new Parameter();
         p.Name  = "text/xml;charset=utf-8";
         p.Value = reqBody;
         p.Type  = ParameterType.RequestBody;
         req.Parameters.Add(p);
         var response = client.Execute(req);
         if (response.StatusCode == HttpStatusCode.OK)
         {
             return(response.Content);
         }
         else
         {
             Log4netUtil.Error("RestRequest执行异常:" + response.Content);
             return(null);
         }
     }
     catch (Exception ex)
     {
         Log4netUtil.Error("RestRequest发生异常:" + ex.Message, ex);
         return(null);
     }
 }
Beispiel #2
0
        public static XmlDocument RemoveNameSpace(string xml, string ns = "xmlns|ns&xsi:|a")
        {
            try
            {
                //去掉注释 和命名空间
                string[] strns = ns.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                if (strns.Length > 0)
                {
                    foreach (var item in strns)
                    {
                        //string r = "(" + item + @":?[^=]*=[""][^""]*[""])";
                        //xml = System.Text.RegularExpressions.Regex.Replace(
                        //  xml,
                        //  r, "",
                        //  System.Text.RegularExpressions.RegexOptions.IgnoreCase |
                        //  System.Text.RegularExpressions.RegexOptions.Multiline);
                        string[] strs = ns.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        xml = xml.Replace(strs[0], strs[1]);
                    }
                }

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xml);
                return(doc);
            }
            catch (Exception ex)
            {
                Log4netUtil.Error("RemoveNameSpace发生异常:" + ex.Message);
                return(null);
            }
        }
Beispiel #3
0
 public static DataTable XmlToDataTable(string parentnode, SchemaInfo info, XmlNode node)
 {
     try
     {
         if (node == null || !node.HasChildNodes)
         {
             return(null);
         }
         if (!string.IsNullOrEmpty(info.CData))
         {
             string strresult = GetNodeTextValue(info.CData, node);
             node = XmlHelper.RemoveNameSpace(strresult, info.RemoveNs);
         }
         DataTable dt     = info.ToDt();
         string[]  pnodes = parentnode.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
         ForeachToTable(0, pnodes, node, dt, info);
         Tools.RemoveEmpty(dt);
         return(dt);
     }
     catch (Exception ex)
     {
         Log4netUtil.Error(ex.Message, ex);
         return(null);
     }
 }
Beispiel #4
0
 public static XmlNodeList GetNodes(string xPath, XmlNode doc)
 {
     try
     {
         XmlNodeList node = doc.SelectNodes(xPath);
         return(node);
     }
     catch (Exception ex)
     {
         Log4netUtil.Error(ex.Message, ex);
         return(null);
     }
 }
 public void BulkDbBySqlServer(string connectionString, string creatTmp, string tmpname, string strSql, DataTable table)
 {
     #region 1.创建临时表
     string strtmpsql = !string.IsNullOrEmpty(creatTmp) ? creatTmp : "truncate table " + tmpname;
     #endregion
     #region 2.将数据使用sqlbulk导入到临时表
     //开始数据保存逻辑
     using (SqlConnection conn = new SqlConnection(connectionString))
     {
         conn.Open();
         SqlCommand cmd = conn.CreateCommand();
         cmd.CommandText = strtmpsql;
         cmd.ExecuteNonQuery();
         //在插入数据的同时检查约束,如果发生错误调用sqlbulkTransaction事务
         SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
         bulkCopy.DestinationTableName = tmpname;                       //代表要插入数据的表名
         foreach (DataColumn dc in table.Columns)                       //传入上述table
         {
             bulkCopy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName); //将table中的列与数据库表这的列一一对应
         }
         try
         {
             bulkCopy.WriteToServer(table);
         }
         catch (Exception ex)
         {
             Log4netUtil.Error("BulkDbBySqlServer批量导入临时表发生异常:" + ex.Message, ex);
             throw new Exception("批量导入临时表发生异常:" + ex.Message);
         }
         finally
         {
             bulkCopy.Close();
         }
         SqlTransaction tran = conn.BeginTransaction();//开启事务
         try
         {
             cmd.Transaction = tran;
             cmd.CommandText = strSql;
             cmd.ExecuteNonQuery();
         }
         catch (Exception ee)
         {
             tran.Rollback();
             Log4netUtil.Error("BulkDbBySqlServer批量导入发生异常:" + ee.Message, ee);
             throw new Exception("批量导入发生异常:" + ee.Message);
         }
         tran.Commit();
     }
     #endregion
 }
Beispiel #6
0
        /// <summary>
        /// 获取Json节点数值
        /// </summary>
        /// <param name="json"></param>
        /// <param name="key"></param>
        /// <param name="defaultvalue"></param>
        /// <returns></returns>
        public static object GetJsonNodeValue(string json, string key, string defaultvalue = "")
        {
            string value = "";

            try
            {
                //Log4netUtil.Info("测试节点");
                //Log4netUtil.Info("解析节点:" + key);
                if (key == "[]")
                {
                    value = json;
                }
                else if (key.Contains("|"))
                {
                    string[] keys       = key.Split('|');
                    JObject  jsonObject = (JObject)JsonConvert.DeserializeObject(json);

                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (i == keys.Length - 1)
                        {
                            value = (jsonObject[keys[i]] ?? defaultvalue).ToString();
                        }
                        else
                        {
                            jsonObject = (JObject)jsonObject[keys[i]];
                        }
                    }
                }
                else
                {
                    JObject jsonObject = (JObject)JsonConvert.DeserializeObject(json);
                    if (string.IsNullOrEmpty(key))
                    {
                        value = jsonObject.ToString();
                    }
                    else
                    {
                        value = (jsonObject[key] ?? defaultvalue).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                Log4netUtil.Error(ex.Message, ex);
            }

            return(value);
        }
 public bool GetTest(string connect)
 {
     try
     {
         using (SqlConnection con = new SqlConnection(connect))
         {
             con.Open();
         }
     }
     catch (Exception ex)
     {
         Log4netUtil.Error(ex.Message);
         return(false);
     }
     return(true);
 }
Beispiel #8
0
        /// <summary>
        /// Json转Dt
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable JsonToDataTable(string json)
        {
            DataTable table = new DataTable();

            try
            {
                //JsonStr为Json字符串
                JArray array = JsonConvert.DeserializeObject(json) as JArray;//反序列化为数组
                if (array.Count > 0)
                {
                    StringBuilder columns = new StringBuilder();

                    JObject objColumns = array[0] as JObject;
                    //构造表头
                    foreach (JToken jkon in objColumns.AsEnumerable <JToken>())
                    {
                        string name = ((JProperty)(jkon)).Name;
                        columns.Append(name + ",");
                        table.Columns.Add(name);
                    }
                    //向表中添加数据
                    for (int i = 0; i < array.Count; i++)
                    {
                        DataRow row = table.NewRow();
                        JObject obj = array[i] as JObject;
                        foreach (JToken jkon in obj.AsEnumerable <JToken>())
                        {
                            string name  = ((JProperty)(jkon)).Name;
                            string value = ((JProperty)(jkon)).Value.ToString();
                            row[name] = value;
                        }
                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
                Log4netUtil.Error(ex.Message, ex);
            }
            return(table);
        }
        public ResultInfo BulkDbBySqlServer(string connectionString, string creatTmp, string tmpname, string strSql, DataTable table, ref ResultInfo retInfo)
        {
            #region 1.创建临时表
            string strtmpsql = "";
            if (!string.IsNullOrEmpty(creatTmp))
            {
                strtmpsql = creatTmp;
            }
            else
            {
                strtmpsql = "truncate table " + tmpname;
            }
            #endregion
            #region 2.将数据使用sqlbulk导入到临时表
            //开始数据保存逻辑

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = strtmpsql;
                cmd.ExecuteNonQuery();
                //在插入数据的同时检查约束,如果发生错误调用sqlbulkTransaction事务
                SqlBulkCopy bulkCopy = new SqlBulkCopy(conn);
                bulkCopy.DestinationTableName = tmpname;                       //代表要插入数据的表名
                foreach (DataColumn dc in table.Columns)                       //传入上述table
                {
                    bulkCopy.ColumnMappings.Add(dc.ColumnName, dc.ColumnName); //将table中的列与数据库表这的列一一对应
                }
                try
                {
                    bulkCopy.WriteToServer(table);
                }
                catch (Exception ex)
                {
                    retInfo.ackcode = "300.2";
                    retInfo.ackflg  = false;
                    retInfo.ackmsg  = "批量导入临时表发生异常:" + ex.Message;
                    Log4netUtil.Error("BulkDbBySqlServer批量导入临时表发生异常:" + ex.Message, ex);
                    return(retInfo);
                }
                finally
                {
                    bulkCopy.Close();
                }
                SqlTransaction tran = conn.BeginTransaction();//开启事务
                try
                {
                    cmd.Transaction = tran;
                    cmd.CommandText = strSql;
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ee)
                {
                    tran.Rollback();
                    retInfo.ackcode = "300.2";
                    retInfo.ackflg  = false;
                    retInfo.ackmsg  = "批量导入发生异常:" + ee.Message;
                    Log4netUtil.Error("BulkDbBySqlServer批量导入发生异常:" + ee.Message, ee);
                    return(retInfo);
                }
                retInfo.ackcode = "100.1";
                retInfo.ackflg  = true;
                retInfo.ackmsg  = "数据库操作成功";
                tran.Commit();
            }
            return(retInfo);

            #endregion
        }
Beispiel #10
0
 public static DataTable XmlToDataTable(string config, XmlNode node)
 {
     try
     {
         if (node == null || !node.HasChildNodes)
         {
             return(null);
         }
         DataTable  dt   = new DataTable();
         SchemaInfo info = JsonConvert.DeserializeObject <SchemaInfo>(config);
         foreach (TableSchema item in info.TableSchema)
         {
             DataColumn dc = new DataColumn(item.column);
             dt.Columns.Add(dc);
         }
         foreach (XmlNode xn in node.ChildNodes)
         {
             if (xn.NodeType == XmlNodeType.Comment)
             {
                 continue;
             }
             DataRow     dr  = dt.NewRow();
             XmlDocument xml = new XmlDocument();
             xml.LoadXml(xn.OuterXml);
             foreach (TableSchema item in info.TableSchema)
             {
                 if (item.maptype == "文本值")
                 {
                     dr[item.column] = XmlHelper.GetNodeTextValue(item.map, xml);
                 }
                 else if (item.maptype == "属性值")
                 {
                     dr[item.column] = XmlHelper.GetNodeAttributeValue(item.map, item.attr, xml);
                 }
                 else if (item.maptype == "关联文本值")
                 {
                     XmlNode glnode = XmlHelper.GetNode(item.relemap, xml);
                     if (glnode != null)
                     {
                         XmlDocument parentxml = new XmlDocument();
                         parentxml.LoadXml(node.ParentNode.OuterXml);
                         dr[item.column] = XmlHelper.GetNodeTextValue(item.map, parentxml);
                     }
                 }
                 else if (item.maptype == "关联属性值")
                 {
                     XmlNode glnode = XmlHelper.GetNode(item.relemap, xml);
                     if (glnode != null)
                     {
                         XmlDocument parentxml = new XmlDocument();
                         parentxml.LoadXml(node.ParentNode.OuterXml);
                         dr[item.column] = XmlHelper.GetNodeAttributeValue(item.map, item.attr, parentxml);
                     }
                 }
                 dt.Rows.Add(dr);
             }
         }
         return(dt);
     }
     catch (Exception ex)
     {
         Log4netUtil.Error(ex.Message, ex);
         return(null);
     }
 }