PubHandle.cs 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;


namespace CsbrHcgClient
{
    class Xml_handle
    {
        private static Mutex mtx = new Mutex();
        //**取配置文件的路径,此文件夹存放于BIN目录下
        public string App_Config(string filename)
        {
            return System.IO.Path.Combine(Application.StartupPath, filename);//取配置文件的目录
        }


        //**作用:DATASET生成.XML文档**//
        public bool createXMLDocument(string filename, DataSet ds, string url)
        {
            XmlDocument xmldoc = new XmlDocument();    //加入声明和跟节点
            string xmlString = "<?xml version=\"1.0\" encoding=\"gb2312\"?><root></root>";
            try
            {
                url = url + "\\" + filename;
                xmldoc.LoadXml(xmlString);
                CreateXMLFile_Schema(xmldoc, ds, url);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public int CreateXMLFile_Schema(XmlDocument document, DataSet ds, string url)
        {
            //建立XML文档模型,并写入数据   
            try
            {
                ds.WriteXml(url, XmlWriteMode.WriteSchema);
            }
            catch
            {
                return 12;
            }
            return 0;

        }


        public int CreateXMLFile(XmlDocument document, DataSet ds, string url)
        {
            //建立XML文档模型,并写入数据    
            try
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    XmlElement table = document.CreateElement(ds.Tables[0].TableName);

                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                    {
                        XmlElement x = document.CreateElement(ds.Tables[0].Columns[j].ColumnName);
                        x.InnerText = ds.Tables[0].Rows[i][j].ToString();
                        table.AppendChild(x);
                    }
                    document.FirstChild.NextSibling.AppendChild(table);
                }
                document.Save(url);
            }
            catch
            {
                return 12;
            }
            return 0;
        }





        /// <summary>
        /// XML转换成DATASET
        /// </summary>
        /// <param name="url">XML文件的名称</param>
        /// <param name="message">返回错误消息</param>
        /// <returns>读取XML的数据集</returns>
        public DataSet ConvertXMLToDataSet_Schema(string url, ref StringBuilder message)
        {
            try
            {
                DataSet xmlDS = new DataSet();
                string filepath = App_Config(url);
                xmlDS.ReadXml(filepath, XmlReadMode.ReadSchema);
                mtx.ReleaseMutex();
                return xmlDS;

            }
            catch (Exception ex)
            {
                message.Append(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// XML转换成DATASET
        /// </summary>
        /// <param name="url">XML文件的名称</param>
        /// <param name="message">返回错误消息</param>
        /// <returns>读取XML的数据集</returns>
        public DataSet ConvertXMLToDataSet(string url, ref StringBuilder message)
        {
            try
            {
                DataSet xmlDS = new DataSet();
                string filepath = App_Config(url);
                xmlDS.ReadXml(filepath);
                return xmlDS;
            }
            catch (Exception ex)
            {
                message.Append(ex.Message);
                return null;
            }
        }

        //**作用:取指定元素指定节点的值,
        //传入值:xmlfilename XML配置文档的名称
        //节点名称:写法限制"//Config[@ID ='" + IDstring + "']" 
        //元素名称:Elemstring
        public string GetValue(string Xmlfilename, string Nodestring, string Elemstring)
        {
            string value = "";
            string Node = "//Config[@ID ='" + Nodestring + "']";
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(App_Config(Xmlfilename));
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode(Node);
                xElem = (XmlElement)xNode.SelectSingleNode(Elemstring);
                if (xElem != null)
                {
                    value = xElem.InnerText;
                    return value;
                }
                else
                {
                    return "";
                }

            }
            catch (Exception)
            {
                return "";
            }
        }

        //**作用:删除节点
        //传入值:xmlfilename XML配置文档的名称
        //节点名称:写法限制"//Config[@ID ='" + IDstring + "']"
        public bool DeletexElem(string Xmlfilename, string IDstring, ref StringBuilder smess)
        {
            string Nodestring = "//Config[@ID ='" + IDstring + "']";
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(App_Config(Xmlfilename));
                XmlElement xElem;
                xElem = (XmlElement)xDoc.SelectSingleNode(Nodestring);
                //如果没有找到,则返回
                if (xElem == null)
                {
                    smess.Append("数据不存在,无需删除!");
                    return false;
                }
                else
                {
                    // 从XML文档中移除元素
                    XmlElement root = xDoc.DocumentElement;
                    root.RemoveChild(xElem);
                }
                xDoc.Save(App_Config(Xmlfilename));
            }
            catch (Exception ex)
            {
                smess.Append(ex.Message);
                return false;
            }
            return true;
        }



        //作用:创建XML文档及根
        //参数:日志文件的目录与文件名称 例:f:\ab\c\d.xml
        public bool CreateLogroot(string xmlnamepath)
        {

            XmlDocument xmldoc = new XmlDocument();    //加入声明和跟节点
            string xmlString = "<?xml version=\"1.0\" encoding=\"gb2312\"?><LOGS></LOGS>";
            try
            {

                xmldoc.LoadXml(xmlString);
                xmldoc.Save(xmlnamepath);
            }
            catch
            {
                return false;
            }
            return true;
        }


        //**作用:新增元素
        public XmlElement addTextElement(XmlDocument doc, XmlElement nodeParent, string strTag, string strValue)
        {
            //通过传入的标签,创建一个新元素
            XmlElement nodeElem = doc.CreateElement(strTag);
            //通过传入的值,创建一个文本节点
            XmlText nodeText = doc.CreateTextNode(strValue);
            // 将此元素作为一个子元素添加到一个元素上
            nodeParent.AppendChild(nodeElem);
            // 将文本节点添加到此元素的子节点中
            nodeElem.AppendChild(nodeText);
            return nodeElem;
        }


        //判断配置文件是否存在
        public int file_exist(string file_name)
        {
            string file_path = App_Config(file_name);
            if (!File.Exists(file_path))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }


    }
   public class pub_handle
    {
        private DataSet ds;
        Xml_handle xmlhandle = new Xml_handle();
        /// <summary>
        /// 取XML文档数据
        /// </summary>
        /// <param name="?">xml文档名字</param>
        /// <returns></returns>
        public DataSet Init(string xml_name, ref StringBuilder smess)
        {
            Xml_handle xmlhandle = new Xml_handle();
            ds = xmlhandle.ConvertXMLToDataSet(xml_name, ref smess);
            if (ds != null)
            {
                return ds;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 返回按检索条件返回的DATATABLE
        /// </summary>
        /// <param name="xml_name">取得的XML的数据</param>
        /// <param name="s_where">数据检索条件</param>
        /// <param name="smess">返回的错误</param>
        /// <returns></returns>       
        public DataTable Init_where(string xml_name, string s_where, ref StringBuilder smess)
        {
            try
            {
                Xml_handle xmlhandle = new Xml_handle();
                ds = xmlhandle.ConvertXMLToDataSet(xml_name, ref smess);
                if (ds != null)
                {
                    DataRow[] dr = ds.Tables[0].Select(s_where);  //重新检索数据行
                    DataTable t_datatable = new DataTable();
                    t_datatable = dr.CopyToDataTable();           //生成新的DATATABLE
                    return t_datatable;
                }
                else
                {
                    smess.Append("数据不存在!");
                    return null;
                }
            }
            catch (Exception ex)
            {
                smess.Append(ex.Message);
                return null;
            }
        }


        /// <summary>
        /// 新增,更新XML文档
        /// </summary>
        /// <param name="ds_xml">需要更新的DATASET</param>
        /// <param name="xml_name">XML文档名</param>
        /// <param name="smess">返回错误信息</param>
        /// <returns></returns>
        public bool Updatexml(DataSet ds_xml, string xml_name, ref StringBuilder smess)
        {

            DataTable dt = ds_xml.Tables[0];
            int changecount = 0;
            XmlDocument xDoc = new XmlDocument();
            try
            {
                if (!File.Exists(xmlhandle.App_Config(xml_name)))
                {
                    xmlhandle.CreateXMLFile_Schema(xDoc, ds_xml, xmlhandle.App_Config(xml_name));
                }
                xDoc.Load(xmlhandle.App_Config(xml_name));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i].RowState != DataRowState.Unchanged)
                    {

                        changecount++;
                        XmlNode xNode;
                        xNode = xDoc.SelectSingleNode("//Config[@ID ='" + dt.Rows[i]["ID"].ToString() + "']");
                        if (xNode != null)
                        {
                            foreach (DataColumn dc in dt.Columns)
                            {
                                if (dc.ColumnName.ToString() != "ID")
                                {
                                    XmlElement xElem_Value = (XmlElement)xNode.SelectSingleNode(dc.ColumnName.ToString());
                                    xElem_Value.InnerText = dt.Rows[i][dc].ToString();
                                }
                            }
                        }
                        else
                        {

                            XmlElement nodeParent = xDoc.DocumentElement;
                            XmlElement elemEmployee = xDoc.CreateElement("Config");
                            elemEmployee.SetAttribute("ID", dt.Rows[i]["ID"].ToString());
                            // 为<Employee>元素添加子元素
                            foreach (DataColumn dc in dt.Columns)
                            {
                                if (dc.ColumnName != "ID")
                                {
                                    xmlhandle.addTextElement(xDoc, elemEmployee, dc.ColumnName.ToString(), dt.Rows[i][dc].ToString());
                                }
                            }

                            nodeParent.AppendChild(elemEmployee);
                        }
                    }
                }


                if (changecount > 0)
                {
                    xDoc.Save(xmlhandle.App_Config(xml_name));
                }
                return true;
            }
            catch (Exception ex)
            {
                smess = smess.Append(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="xml_name"></param>
        /// <param name="node_id"></param>
        /// <param name="smess"></param>
        /// <returns></returns>
        public bool Delxml(string xml_name, string node_id, ref StringBuilder smess)
        {
            bool del_result = xmlhandle.DeletexElem(xml_name, node_id, ref smess);
            return del_result;
        }

        /// <summary>
        /// 用于保存前检验数据ID是否重复
        /// </summary>
        /// <param name="ds">要检验的数据集</param>
        /// <returns>TURE重复,FALSE不重复</returns>
        public bool ID_CHECK(DataSet ds, ref StringBuilder smess)
        {
            var xmlquery = from xml in ds.Tables[0].AsEnumerable()
                           group xml by xml.Field<string>("ID") into g
                           select new { IDstring = g.Key, IdCount = g.Count() };
            foreach (var checknum in xmlquery)
            {
                if (checknum.IdCount > 1)
                {
                    smess.Append("配置:" + checknum.IDstring + "重复,请修改后提交");
                    return true;
                }
            }
            return false;

        }


        //设置服务地址配置文件的值
        public bool SetValue(string xml_name, string AppKey, string AppValue, string appchoose, ref string errmess)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlhandle.App_Config(xml_name));
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//services");
                xElem = (XmlElement)xNode.SelectSingleNode("//service[@name ='" + appchoose + "']//host//baseAddresses//add");
                if (xElem != null)
                {
                    xElem.SetAttribute(AppKey, AppValue);
                }
                else
                {
                    return false;
                }
                xDoc.Save(xmlhandle.App_Config(xml_name));
                return true;
            }
            catch (Exception ex)
            {
                errmess = ex.Message;
                return false;
            }
        }

    }
}