File Structure : Xon - escaco95/Charcoal GitHub Wiki

Idea

TreeView
XON -> OBJ,LST,VAR can be added to this
Display as : <XON>
OBJ -> PARAM-OBJ,PARAM-LST,PARAM-VAR can be added to this
Display as : {OBJECT}
LST -> OBJ,LST,VAR can be added to this
Display as : [LIST]
VAR -> Leaf node
Display as : Value

PARAM-OBJ -> PARAM-OBJ,PARAM-LST,PARAM-VAR can be added to this
Display as : "KeyName" : {OBJECT}
PARAM-LST -> OBJ,LST,VAR can be added to this
Display as : "KeyName" : [LIST]
PARAM-VAR -> Leaf node
Display as : "KeyName" : Value

Xon Restriction Rule

// Copyright (c) 2019 최선웅
// Distributed under the BSD License, Version 1.0.

Rule Based Xon Editor

// Copyright (c) 2019 최선웅
// Distributed under the BSD License, Version 1.0.

Xon Source Code

// Copyright (c) 2019 최선웅
// Distributed under the BSD License, Version 1.0.
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Xon
{
    public enum XonElementType
    {
        Unknown = 0,
        None = Unknown,
        Invalid = Unknown,
        Object,
        List,
        Array = List,
        Value
    }
    public abstract class XonElement
    {
        public XonElementType Type { get; protected set; }
        public override string ToString() => ToString(false);
        public virtual string ToString(bool formatted = false)
        {
            var XmlDocument = ToXmlDocument();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = formatted
                ,
                IndentChars = "  "
                ,
                NewLineChars = System.Environment.NewLine
                ,
                NewLineHandling = NewLineHandling.Replace
                //,NewLineOnAttributes = true
                //,OmitXmlDeclaration = false
            };
            StringBuilder output = new StringBuilder();

            using (XmlWriter writer = XmlWriter.Create(output,settings))
                XmlDocument.Save(writer);

            return output.ToString();
        }
        public XmlDocument ToXmlDocument() => ToXmlDocument(null);
        public virtual XmlDocument ToXmlDocument(XmlElement xmlElement = null) { throw new NotImplementedException(); }
    }
    public class XonObject : XonElement
    {
        public Dictionary<string, XonElement> Tuples { get; protected set; }
        public bool Contains(string key) { return Tuples.ContainsKey(key); }
        public void Remove(string key) { Tuples.Remove(key); }
        public void Clear() { Tuples.Clear(); }
        public XonElement this[string key] { get { return Tuples[key]; } set { Tuples[key] = value; } }
        public void Add(string key, XonElement Element) { Tuples.Add(key, Element); }
        public void Add(string key, string value) { Tuples.Add(key, new XonValue() { RawValue = value }); }
        public void Add(string key, object value) { Tuples.Add(key, new XonValue() { RawValue = value.ToString() }); }
        public XonObject()
        {
            Type = XonElementType.Object;
            Tuples = new Dictionary<string, XonElement>();
        }
        public override XmlDocument ToXmlDocument(XmlElement xmlElement = null)
        {
            XmlDocument xmlDocument;
            if (xmlElement != null)
                xmlDocument = xmlElement.OwnerDocument;
            else
            {
                xmlDocument = new XmlDocument();
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty));
                var rootElement = xmlDocument.CreateElement("XON");
                xmlElement = rootElement;
                xmlDocument.AppendChild(rootElement);
            }

            var curElement = xmlDocument.CreateElement("OBJECT");
            foreach(var tuple in Tuples)
            {
                var keyElement = xmlDocument.CreateElement("KEY");
                keyElement.SetAttribute("name", tuple.Key);
                tuple.Value.ToXmlDocument(keyElement);
                curElement.AppendChild(keyElement);
            }
            xmlElement.AppendChild(curElement);

            return xmlDocument;
        }
    }
    public class XonList : XonElement
    {
        public List<XonElement> Items { get; protected set; }
        public bool Contains(XonElement Element) { return Items.Contains(Element); }
        public void RemoveAt(int index) { Items.RemoveAt(index); }
        public void Clear() { Items.Clear(); }
        public XonElement this[int index] { get { return Items[index]; } set { Items[index] = value; } }
        public void Add(XonElement Element) { Items.Add(Element); }
        public void Add(string value) { Items.Add(new XonValue() { RawValue = value }); }
        public void Add(object value) { Items.Add(new XonValue() { RawValue = value.ToString() }); }
        public XonList()
        {
            Type = XonElementType.List;
            Items = new List<XonElement>();
        }
        public override XmlDocument ToXmlDocument(XmlElement xmlElement = null)
        {
            XmlDocument xmlDocument;
            if (xmlElement != null)
                xmlDocument = xmlElement.OwnerDocument;
            else
            {
                xmlDocument = new XmlDocument();
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty));
                var rootElement = xmlDocument.CreateElement("XON");
                xmlElement = rootElement;
                xmlDocument.AppendChild(rootElement);
            }

            var curElement = xmlDocument.CreateElement("LIST");
            foreach (var item in Items)
            {
                var keyElement = xmlDocument.CreateElement("ITEM");
                item.ToXmlDocument(keyElement);
                curElement.AppendChild(keyElement);
            }
            xmlElement.AppendChild(curElement);

            return xmlDocument;
        }
    }
    public class XonValue : XonElement
    {
        public string RawValue { get; set; }
        public string TypeIdentifier { get; set; }
        public XonValue()
        {
            Type = XonElementType.Value;
            RawValue = string.Empty;
            TypeIdentifier = string.Empty;
        }
        public override XmlDocument ToXmlDocument(XmlElement xmlElement = null)
        {
            XmlDocument xmlDocument;
            if (xmlElement != null)
                xmlDocument = xmlElement.OwnerDocument;
            else
            {
                xmlDocument = new XmlDocument();
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", string.Empty));
                var rootElement = xmlDocument.CreateElement("XON");
                xmlElement = rootElement;
                xmlDocument.AppendChild(rootElement);
            }

            var curElement = xmlDocument.CreateElement("VALUE");
            if(!string.IsNullOrEmpty(TypeIdentifier))
                curElement.SetAttribute("type", TypeIdentifier);
            curElement.SetAttribute("value", RawValue);
            xmlElement.AppendChild(curElement);

            return xmlDocument;
        }
    }
}

Example Usage

// Copyright (c) 2019 최선웅
// Distributed under the BSD License, Version 1.0.
private void Form1_Load(object sender, EventArgs e)
{
    var xarr = new XonList();
    xarr.Add("68");
    xarr.Add("68.0");
    xarr.Add("\"This is string.{STFU<>}\r\n\tA\"");
    var xon = new XonObject();
    xon.Add("key", "value");
    xon.Add("key2", xarr);
    System.Console.WriteLine($"{xon.ToString(true)}");
}
⚠️ **GitHub.com Fallback** ⚠️