//-----------------------------------------------------------------------
        private static XElement DefinitionToElement(DataDefinition def)
        {
            if (def is StringDefinition)
            {
                var el = new XElement("Data");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "String"));

                return(el);
            }
            else if (def is FileDefinition)
            {
                var el = new XElement("Data");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "File"));

                return(el);
            }
            else if (def is BooleanDefinition)
            {
                var el = new XElement("Data");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "Boolean"));

                return(el);
            }
            else if (def is NumberDefinition)
            {
                var ndef = def as NumberDefinition;

                var el = new XElement("Data");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("Min", ndef.MinValue));
                el.Add(new XAttribute("Max", ndef.MaxValue));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                if (ndef.UseIntegers)
                {
                    el.Add(new XAttribute("Type", "int"));
                }
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "Number"));

                return(el);
            }
            else if (def is EnumDefinition)
            {
                var ndef = def as EnumDefinition;

                var el = new XElement("Data");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute("EnumValues", string.Join(",", ndef.EnumValues.OrderBy(e => e))));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "Enum"));

                return(el);
            }
            else if (def is StructDefinition)
            {
                var ndef = def as StructDefinition;

                var el = new XElement("Definition");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "Struct"));

                var hasNameChild = false;
                foreach (var cdef in ndef.Children)
                {
                    var cel = DefinitionToElement(cdef);
                    cel.Name = "Data";

                    if (cdef.Name.ToLower() == "name")
                    {
                        hasNameChild = true;
                    }

                    el.Add(cel);
                }

                if (hasNameChild)
                {
                    el.Add(new XAttribute("Description", "{name}"));
                }

                return(el);
            }
            else if (def is CollectionDefinition)
            {
                var ndef = def as CollectionDefinition;

                var el = new XElement("Definition");
                el.Add(new XAttribute("Name", def.Name));
                el.Add(new XAttribute("SkipIfDefault", "false"));
                el.Add(new XAttribute(DataDefinition.MetaNS + "RefKey", "Collection"));

                var cel = DefinitionToElement(ndef.ChildDefinitions[0].WrappedDefinition);
                cel.Name = "Item";

                el.Add(cel);

                return(el);
            }

            throw new Exception("Forgot to handle definition of type: " + def.GetType());
        }