Exemplo n.º 1
0
        /// <inheritdoc />
        protected override void OnSave()
        {
            if (string.IsNullOrEmpty(_saveFileName))
            {
                throw new FileNotFoundException("SaveFileName was not declared.", "SaveFileName");
            }
            using (StreamWriter sw =
                       new StreamWriter(new FileStream(_saveFileName, FileMode.OpenOrCreate, FileAccess.Write)))
            {
                // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
                foreach (IniConfig cfg in _configs.Values)
                {
                    if (cfg.Infos != null)
                    {
                        foreach (string info in cfg.Infos)
                        {
                            sw.WriteLine(info);
                        }
                    }
                    sw.WriteLine(
                        $"[{cfg.Name}]{(string.IsNullOrEmpty(cfg.Comment) ? "" : $" {IniParser.ESCAPE_COMMENT}{cfg.Comment}")}");
                    foreach (KeyValuePair <string, ValueCommentPair> item in cfg.VcPairs)
                    {
                        ValueCommentPair pair = item.Value;

                        if (cfg.KeyInfos.TryGetValue(item.Key, out string[] infos))
Exemplo n.º 2
0
        /// <inheritdoc />
        protected override void OnSave()
        {
            if (string.IsNullOrEmpty(_saveFileName))
            {
                throw new FileNotFoundException("SaveFileName was not declared.", "SaveFileName");
            }

            XmlDocument doc  = new XmlDocument();
            XmlNode     root = doc.CreateElement("config");

            doc.AppendChild(root);

            // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
            foreach (XmlConfig cfg in _configs.Values)
            {
                if (cfg.Infos != null)
                {
                    foreach (string info in cfg.Infos)
                    {
                        XmlComment commentNode = doc.CreateComment(info);
                        root.AppendChild(commentNode);
                    }
                }
                XmlNode section = CreateSectionNode(doc, cfg.Name, cfg.Comment);
                root.AppendChild(section);
                foreach (KeyValuePair <string, ValueCommentPair> item in cfg.VcPairs)
                {
                    ValueCommentPair pair = item.Value;
                    XmlNode          kvc  = CreateKvcNode(doc, item.Key, pair.Value, pair.Comment);
                    section.AppendChild(kvc);
                }
            }

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Encoding           = Encoding.UTF8,
                ConformanceLevel   = ConformanceLevel.Document,
                OmitXmlDeclaration = false,
                CloseOutput        = true,
                Indent             = true,
                IndentChars        = "\t",
                NewLineHandling    = NewLineHandling.Replace
            };

            using (XmlWriter writer = XmlWriter.Create(_saveFileName, settings))
            {
                doc.WriteContentTo(writer);
            }
        }