XML應用

摘要:XML應用

##########################  XML表建立     #################################

        #region < XML表建立 >
        public void xmlCreate()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
            XmlElement xmlroot = xdoc.CreateElement("MemberArrary");
            xdoc.AppendChild(xmlroot);

            // 將建立的 XML 節點儲存為檔案
            xdoc.Save(FilePath);
        }
        #endregion

 

##########################  新增XML資料  #################################

        #region < 新增XML資料 >
        /// <summary>
        /// 新增XML資料
        /// </summary>
        /// <param name="NodeName">節點名稱</param>
        /// <param name="Name">姓名</param>
        /// <param name="Phone">聯絡電話</param>
        /// <param name="Email">Email</param>
        /// <returns></returns>
        public bool xmlInsert(string NodeName, string Name, string Phone, string Email, string pwd)
        {
            XmlDocument xdoc = new XmlDocument();

            if (!File.Exists(FilePath))//抓取不到檔案,則建立檔案
                xmlCreate();
            try
            {
                xdoc.Load(FilePath);//讀取XML資料

                XmlNode XRootNode = xdoc.SelectSingleNode("//MemberArrary");//抓取COMPANY公司資料

                //XmlElement memberinfo = xdoc.CreateElement("Member");
                XmlElement memberinfo = xdoc.CreateElement(NodeName);


                XmlElement NameChileID = xdoc.CreateElement("ID");
                NameChileID.InnerText = StringCrypt.Encrypt(Gene.Trim(NodeName));
                memberinfo.AppendChild(NameChileID);

                XmlElement NameChileName = xdoc.CreateElement("Name");
                NameChileName.InnerText = StringCrypt.Encrypt(Gene.Trim(Name));
                memberinfo.AppendChild(NameChileName);

                XmlElement NameChileEmail = xdoc.CreateElement("Email");
                NameChileEmail.InnerText = StringCrypt.Encrypt(Gene.Trim(Email));
                memberinfo.AppendChild(NameChileEmail);

                XmlElement NameChilePhone = xdoc.CreateElement("Phone");
                NameChilePhone.InnerText = StringCrypt.Encrypt(Gene.Trim(Phone));
                memberinfo.AppendChild(NameChilePhone);

                XmlElement NameChilePwd = xdoc.CreateElement("Pwd");
                NameChilePwd.InnerText = StringCrypt.Encrypt(Gene.Trim(pwd));
                memberinfo.AppendChild(NameChilePwd);


                XRootNode.AppendChild(memberinfo);

                // 將建立的 XML 節點儲存為檔案
                xdoc.Save(FilePath);
                return true;
            }
            catch
            { return false; }
        }
        #endregion

 

 

 ########################## 修改xml節點資料 #################################

        #region < 修改xml節點資料  >
        public bool xmlupdate(string NodeName, string Name, string Email, string Phone, string pwd)
        {
            XmlDocument xdoc = new XmlDocument();
            try
            {
                xdoc.Load(FilePath);//讀取XML資料

                if (Gene.Trim(Name) != "")  //修改Name
                    xdoc.SelectSingleNode("//" + NodeName).SelectSingleNode("//Name").InnerText = StringCrypt.Encrypt(Name);

                if (Gene.Trim(Email) != "") //修改Email
                    xdoc.SelectSingleNode("//" + NodeName).SelectSingleNode("//Email").InnerText = StringCrypt.Encrypt(Email);

                if (Gene.Trim(Phone) != "")//修改Phone
                    xdoc.SelectSingleNode("//" + NodeName).SelectSingleNode("//Phone").InnerText = StringCrypt.Encrypt(Phone);

                if (Gene.Trim(pwd) != "")//修改Phone
                    xdoc.SelectSingleNode("//" + NodeName).SelectSingleNode("//Phone").InnerText = StringCrypt.Encrypt(pwd);

                xdoc.Save(FilePath);//儲存XML檔

                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion

 

 

########################## 刪除xml節點的子節點資料 #################################

        #region < 刪除xml節點的子節點資料  >
        public bool xmlDeleteNodeChildNode(string NodeName)
        {
            XmlDocument xdoc = new XmlDocument();
            try
            {
                xdoc.Load(FilePath);//讀取XML資料
                xdoc.SelectSingleNode("//" + NodeName).RemoveAll();
                xdoc.Save(FilePath);
                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion

 

 

##########################  讀取單一節點 #################################

        #region < 讀取單一節點 >
        public csCloumns xmlReadNode(string NodeName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);

            XmlNode xd = doc.SelectSingleNode("//" + NodeName);

            csCloumns ocolumn = new csCloumns();
            ocolumn.ID = StringCrypt.Decrypt(xd["ID"].InnerText);
            ocolumn.Name = StringCrypt.Decrypt(xd["Name"].InnerText);
            ocolumn.Phone = StringCrypt.Decrypt(xd["Phone"].InnerText);
            ocolumn.Email = StringCrypt.Decrypt(xd["Email"].InnerText);
            ocolumn.Pwd = StringCrypt.Decrypt(xd["Pwd"].InnerText);
            return ocolumn;
        }
        #endregion

 

########################## 修改單一節點 #################################

        #region < 修改單一節點 >
        public bool xmlSetNode(string NodeName, csCloumns ocolumn)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(FilePath);

                XmlNode xd = doc.SelectSingleNode("//" + NodeName);
                if (Gene.Trim(ocolumn.ID)!="") xd["ID"].InnerText = StringCrypt.Encrypt(Gene.Trim(ocolumn.ID));
                if (Gene.Trim(ocolumn.Name) != "") xd["Name"].InnerText = StringCrypt.Encrypt(Gene.Trim(ocolumn.Name));
                if (Gene.Trim(ocolumn.Phone) != "") xd["Phone"].InnerText = StringCrypt.Encrypt(Gene.Trim(ocolumn.Phone));
                if (Gene.Trim(ocolumn.Email) != "") xd["Email"].InnerText = StringCrypt.Encrypt(Gene.Trim(ocolumn.Email));
                if (Gene.Trim(ocolumn.Pwd) != "") xd["Pwd"].InnerText = StringCrypt.Encrypt(Gene.Trim(ocolumn.Pwd));
                doc.Save(FilePath);
                return true;
            }
            catch { return false; }
        }
        #endregion

 

########################## xml轉成DataTable  #################################

        #region < xml轉成DataTable >
        public DataTable xmlConvertTable()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);
            // XmlReader XR = XmlReader.Create(new System.IO.StringReader(doc.OuterXml));
            // DataSet ds = new DataSet();
            // ds.ReadXml(XR);
            // DataTable dt = ds.Tables[0].Clone();

            DataTable dt = new DataTable();
            dt.Columns.Add("ID", System.Type.GetType("System.String"));
            dt.Columns.Add("Name", System.Type.GetType("System.String"));
            dt.Columns.Add("Email", System.Type.GetType("System.String"));
            dt.Columns.Add("Phone", System.Type.GetType("System.String"));

 

            XmlNode xnode = doc.SelectSingleNode("//MemberArrary");
            XmlNodeList list = xnode.ChildNodes;
            //("//MemberArrary");

            foreach (XmlNode xd in list)
            {
                if (!xd.HasChildNodes) continue;

                DataRow Newdr = dt.NewRow();
                Newdr["ID"] = StringCrypt.Decrypt(Gene.Trim(xd["ID"].InnerText));
                Newdr["Name"] = StringCrypt.Decrypt(Gene.Trim(xd["Name"].InnerText));
                Newdr["Email"] = StringCrypt.Decrypt(Gene.Trim(xd["Email"].InnerText));
                Newdr["Phone"] = StringCrypt.Decrypt(Gene.Trim(xd["Phone"].InnerText));
                dt.Rows.Add(Newdr);
            }

            // ds.Dispose();
            return dt;

        }
        #endregion

 

########################## 利用Application 機制配合xml儲存 取得流水號  #################################

        #region < GetMaxCount 取得Idienty  >
        public Int64 GetMaxCount(Page _Page)
        {
            string CountFilePath = HttpContext.Current.Server.MapPath("~/App_Data/Count.xml");
            XmlDocument xdoc = new XmlDocument();
            if (!File.Exists(CountFilePath))//抓取不到檔案,則建立檔案
                return -1;
            try
            {
                xdoc.Load(CountFilePath);//讀取XML資料
                _Page.Application.Lock();
                Int64 count = Convert.ToInt64(xdoc.SelectSingleNode("//MemMaxCount").InnerText);//抓取COMPANY公司資料
                xdoc.SelectSingleNode("//MemMaxCount").InnerText = (count + 1).ToString();
                xdoc.Save(CountFilePath);
                _Page.Application.UnLock();
                return count;
            }
            catch
            { return -1; }

        }
        #endregion