Пример #1
0
        public void SaveEncodedFile(string xFile, string xData, bool isMod = false, bool saveNow = false)
        {
            // if (!App.m_bxmlCacheEnable || xFile== App.AppLicenseFile
            //   || xFile ==App.AppLicenseFile_OLD || xFile== App.AppInfoFile) {
            //  SaveEncodedFile_old(xFile, xData); return;
            //   }
            string      item = xFile.Split('\\').Last();
            XmlDocument d    = new XmlDocument();
            XMLELEMENT  el;

            try { d.LoadXml(xData); }
            catch (Exception ex)
            {
                string log = ex.Message;
                xData = xData.ToString().Replace("&", "&").Replace("\"", "'");
                d.LoadXml(xData);
            }
            if (App.m_xmlCache.ContainsKey(item))
            {
                el     = (XMLELEMENT)(App.m_xmlCache[item]);
                el.doc = d; el.isModified = isMod; App.m_xmlCache[item] = el;
            }
            else
            {
                CheckXmlCache(); App.m_xmlCache.Add(item, new XMLELEMENT(d, isMod));
            }
            //if (!isMod || !saveNow) return;
            if (!saveNow)
            {
                return;
            }
            // file xml in d, chiave in item
            // [1] creare albero ridotto con i soli nodi modificati
            XmlDocument doc = new XmlDocument();

            doc = StaticUtilities.ExtractMasterFileMod(d);
            if (doc == null)
            {
                doc = StaticUtilities.ExtractTreeMod(d);
            }
            if (doc == null)
            {
                doc = StaticUtilities.ExtractDatiMod(d);
            }
            if (doc == null)
            {
                return;        // just to be sure
            }
            // [2] scrivere dati in dbo.xmlSaveTest
            using (SqlConnection conn = new SqlConnection(App.connString))
            {
                string query, str;
                str   = doc.OuterXml.Replace("'", "''");
                query = string.Format(
                    "insert into xmlSaveTest (guid,data)\n" + "values ('{0}','{1}')", item, str);
                conn.Open();
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.CommandTimeout = App.m_CommandTimeout;
                try { cmd.ExecuteNonQuery(); }
                catch (Exception ex)
                {
                    if (!App.m_bNoExceptionMsg)
                    {
                        string msg = "SaveEncodedFile(): errore\n" + ex.Message;
                        MessageBox.Show(msg);
                    }
                }
            }
            // [3] invocare dbo.SaveSingleModified passando guid=item
            using (SqlConnection conn = new SqlConnection(App.connString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("dbo.SaveSingleModified", conn);
                cmd.Parameters.AddWithValue("@guid", item);
                cmd.CommandType    = CommandType.StoredProcedure;
                cmd.CommandTimeout = App.m_CommandTimeout;
                try { cmd.ExecuteNonQuery(); }
                catch (Exception ex)
                {
                    if (!App.m_bNoExceptionMsg)
                    {
                        string msg = "dbo.SaveSingleModified: errore\n" + ex.Message;
                        MessageBox.Show(msg);
                    }
                }
            }
            // [4] eliminare flag di modifica dalla cache
            //     SQL elimina XML dalla sua cache
            XmlAttributeCollection attrColl = d.DocumentElement.Attributes;

            attrColl.RemoveNamedItem("nodeModified");
            attrColl.RemoveNamedItem("idFather");
            el     = (XMLELEMENT)(App.m_xmlCache[item]);
            el.doc = d; el.isModified = false; App.m_xmlCache[item] = el;
        }