AmazedSaint.Elastic.DynamicExtensions.XElementFromElastic C# (CSharp) Method

XElementFromElastic() public static method

Returns an XElement from an ElasticObject
public static XElementFromElastic ( ElasticObject elastic, System.Xml.Linq.XNamespace nameSpace = null ) : System.Xml.Linq.XElement
elastic AmazedSaint.Elastic.Lib.ElasticObject
nameSpace System.Xml.Linq.XNamespace
return System.Xml.Linq.XElement
        public static XElement XElementFromElastic(ElasticObject elastic, XNamespace nameSpace = null)
        {
            // we default to empty namespace
            nameSpace = nameSpace ?? string.Empty;
            var exp = new XElement(nameSpace + elastic.InternalName);

            foreach (var a in elastic.Attributes)
            {
                if (a.Value.InternalValue != null)
                {
                    // if we have xmlns attribute add it like XNamespace instead of regular attribute
                    if (a.Key.Equals("xmlns", StringComparison.OrdinalIgnoreCase))
                    {
                        nameSpace = a.Value.InternalValue.ToString();
                        exp.Name = nameSpace.GetName(exp.Name.LocalName);
                    }
                    else
                    {
                        exp.Add(new XAttribute(a.Key, a.Value.InternalValue));
                    }
                }
            }

            if (null != elastic.InternalContent && elastic.InternalContent is string)
            {
                exp.Add(new XText(elastic.InternalContent as string));
            }

            foreach (var c in elastic.Elements)
            {
                    // for child element add current XNamespace
                    var child = XElementFromElastic(c, nameSpace);
                    exp.Add(child);
            }
            return exp;
        }