Example #1
0
        /// <summary>
        /// Enregistre la liste dans un fichier config xml
        /// </summary>
        /// <param name="fichierXML"></param>
        public void SerializeToFile(ParamValues datavalue, string fichierXML)
        {
            try
            {
                // vérif et convert
                ParamValues dvforxml = datavalue.Clone();
                foreach (ParamValuesNode itemd in dvforxml.GetList())
                {
                    if (itemd.Name.Length > 0 && itemd.Name[0] != '/')
                    {
                        itemd.Name = "/noparam/" + itemd.Name;
                    }
                }

                XmlTextWriter XmlTextWriter = new XmlTextWriter(fichierXML, System.Text.Encoding.UTF8);
                XmlTextWriter.Formatting = Formatting.Indented;

                XmlTextWriter.WriteStartDocument();

                XmlTextWriter = savewriteburn(XmlTextWriter, dvforxml);

                XmlTextWriter.Flush(); //vide le buffer
                XmlTextWriter.Close(); // ferme le document
            }
            finally
            {
            }
        }
Example #2
0
 public void Fusion(ParamValues dataext, bool ecraser = false)
 {
     foreach (ParamValuesNode itemd in dataext.GetList())
     {
         ParamValuesNode itemsearch = this.Get(itemd.Name);
         if (itemsearch != null && itemsearch.Name != "")
         {
             if (ecraser)
             {
                 this.Add(itemd.Clone());
             }
         }
         else
         {
             this.Add(itemd.Clone());
         }
     }
 }
Example #3
0
        public string Serialize(ParamValues datavalue)
        {
            try
            {
                ParamValuesNodeHierarchical firstnode = ParamValuesTools.GetHierarchicalNodes(datavalue.GetList());
                //Newtonsoft.Json.Linq.JToken jobject = Serialize_Recursif(firstnode,true);

                //Newtonsoft.Json.Formatting outjsonformating = Newtonsoft.Json.Formatting.None;
                //string jsontstr = jobject.ToString(outjsonformating);
                ////jsontstr = jsontstr.Replace("\r\n", "");//.Replace("\"", "");
                ////jsontstr = System.Text.RegularExpressions.Regex.Unescape(jsontstr);
                //return jsontstr;

                var options = new JsonWriterOptions()
                {
                    Indented = ConfigIndented
                };
                using (var stream = new MemoryStream())
                {
                    using (var writer = new Utf8JsonWriter(stream, options))
                    {
                        // on écrit pas le premier noeud param
                        writer.WriteStartObject();
                        foreach (var subnode in firstnode.ChildrenNodes)
                        {
                            Serialize_Recursif(writer, subnode);
                        }
                        writer.WriteEndObject();
                    }
                    string json = Encoding.UTF8.GetString(stream.ToArray());
                    return(json);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("SerializeJson " + ex.Message, ex);
            }
        }
Example #4
0
        /// <summary>
        /// Enregistre la liste dans un fichier config xml
        /// </summary>
        /// <param name="fichierXML"></param>
        public string Serialize(ParamValues datavalue)
        {
            string fluxxml = ""; //chaine xml final

            if (datavalue.Count() == 0)
            {
                return("<?xml version=\"1.0\" ?><param><empty>True</empty></param>");
            }


            ParamValues dvforxml = null; // datavalue buffer

            try
            {
                try
                {
                    // vérif et convert
                    dvforxml = datavalue.Clone();
                    try { dvforxml.Sort(); } catch { }
                    foreach (ParamValuesNode itemd in dvforxml.GetList())
                    {
                        if (itemd.Name.Length > 0 && itemd.Name[0] != '/')
                        {
                            itemd.Name = "/noparam/" + itemd.Name;
                        }
                    }
                }
                catch (Exception ev)
                {
                    throw new Exception("Verif : " + ev.Message);
                }

                StringBuilder builder = new StringBuilder();
                using (StringWriter stringWriter = new StringWriter(builder))
                {
                    XmlTextWriter writer = new XmlTextWriter(stringWriter);

                    // This produces UTF16 XML
                    writer.Indentation = 1;
                    writer.IndentChar  = '\t';
                    writer.Formatting  = Formatting.Indented;

                    writer.WriteStartDocument();
                    writer = savewriteburn(writer, dvforxml);

                    writer.WriteEndDocument();
                    writer.Close();
                }


                fluxxml = builder.ToString();
            }
            catch (Exception e)
            {
                throw new Exception("Datavalue toFluxXml : " + e, e);
            }
            finally
            {
            }


            //if (this.datavalue.isEncrypted) fluxxml = cryptageFlux(fluxxml);
            return(fluxxml);
        }
Example #5
0
        private XmlTextWriter savewriteburn(XmlTextWriter XmlTextWriter, ParamValues databurn)
        {
            string[] exnode     = new string[1];
            bool     start      = true;
            bool     exburn     = false;
            int      nodeouvert = 0;
            int      iielement  = 1;
            // ex :  /root/general/param/ex/data/
            // ex : /root/societe/liste/data/
            int ii = 0;

            foreach (ParamValuesNode datatab in databurn.GetList())
            {
                string namet = datatab.Name;
                if (namet[0] == '/')
                {
                    namet = namet.Substring(1);
                }
                if (namet[namet.Length - 1] == '/')
                {
                    namet = namet.Substring(0, namet.Length - 1);
                }
                int namti = namet.Split('/').Length;

                // on cree la premiere donnee
                if (start)
                {
                    exnode = namet.Split('/');
                    ii     = 0;
                    foreach (string item in namet.Split('/'))
                    {
                        //if (ii != namet.Split('/').Length - 1)
                        if (ii == namti - 1) // -1 pour taille -> index du tableau
                        {
                            XmlTextWriter = savewritenode(XmlTextWriter, datatab, item);
                            //if (datatab.type == "Data.Burn") { exburn = true; }// bricolage du bug quil ferme pas tout ...


                            //XmlTextWriter.WriteEndElement();
                            nodeouvert++;
                        }
                        else
                        {
                            XmlTextWriter.WriteStartElement(item); nodeouvert++;
                        }
                        ii++;
                    }


                    start = false;
                    continue;
                }



                // on ferme les node en trops
                int comptage = 0;
                ii = 0;
                foreach (string item in namet.Split('/'))
                {
                    if (item != exnode[ii])
                    {
                        comptage = exnode.Length - ii; // -1?
                        break;
                    }
                    ii++;
                }
                int comptage2 = comptage;
                for (ii = 0; comptage > 0; comptage--)
                {
                    XmlTextWriter.WriteEndElement();
                    nodeouvert--;
                }



                // on creer les nouveau node et la donnée

                ii = 0;
                foreach (string item in namet.Split('/'))
                {
                    if ((ii > nodeouvert - 1) || (exburn && ii >= nodeouvert - 1)) //&& ii != namti
                    {
                        if (ii == namti - 1)                                       // -1 pour taille -> index du tableau
                        {
                            if (exburn)
                            {
                                XmlTextWriter.WriteEndElement();         // bricolage ...
                            }
                            exburn        = false;
                            XmlTextWriter = savewritenode(XmlTextWriter, datatab, item);
                            //if (datatab.type == "Data.Burn") { exburn = true; } // bricolage du bug quil ferme pas tout ...

                            //XmlTextWriter.WriteEndElement();
                            nodeouvert++;
                            exnode = namet.Split('/');
                        }
                        else
                        {
                            XmlTextWriter.WriteStartElement(item);
                            nodeouvert++;
                        }
                    }

                    ii++;
                }



                // si c'est la derniere donnée on ferme tout
                if (databurn.Count() == iielement)
                {
                    while (nodeouvert > 0)
                    {
                        XmlTextWriter.WriteEndElement();
                        nodeouvert = nodeouvert - 1;
                    }
                }
                iielement++;
            }

            return(XmlTextWriter);
        }