Beispiel #1
0
        private bool TrySetKeyValue(string key, object value, bool add)
        {
            var xe = this.Get(key);

            if (xe != null && add)
            {
                throw new ArgumentException("An element with the same key already exists.");
            }
            if (value == null)
            {
                if (xe != null)
                {
                    xe.Remove();
                    this.EnsureArrayType();
                }
                return(true);
            }

            var type = GetTomlType(value);
            var attr = CreateTypeAttr(type);
            var node = ToXml(type, value);

            if (xe == null)
            {
                this.element.Add(XUtils.CreateElement(key, attr, node));
            }
            else
            {
                xe.SetAttributeValue("type", attr[0].Value);
                xe.SetAttributeValue("toml", attr[1].Value);
                xe.ReplaceNodes(node);
            }
            return(true);
        }
 private static XElement CreateTableElement(string name, IEnumerable <TableNode> nodes)
 {
     return(XUtils.CreateElement(name,
                                 new XAttribute("type", "object"),
                                 new XAttribute("toml", TomlItemType.Table.ToString()),
                                 ToXNodes(nodes)
                                 ));
 }
            public XElement ToXElement(string name)
            {
                var xe = CreateTableElement(name, this.Nodes);

                foreach (var kvp in this.ArrayOfTables)
                {
                    xe.Add(XUtils.CreateElement(kvp.Key,
                                                new XAttribute("type", "array"),
                                                new XAttribute("toml", TomlItemType.Array.ToString()),
                                                kvp.Value.Select(t => t.ToXElement("item"))
                                                ));
                }
                xe.Add(this.Children.Select(kvp => kvp.Value.ToXElement(kvp.Key)));
                return(xe);
            }