public static object CreateJsFromXElement <TElement, TAttribute, TObject>(IElementProvider <TElement, TAttribute, TObject> elementProvider, TElement xelement) { var index = new Dictionary <string, object>(); index["name"] = elementProvider.LocalName(xelement); if (elementProvider.Namespace(xelement) != null) { index["ns"] = elementProvider.Namespace(xelement); } var attrs = elementProvider.Attributes(xelement).Select(attr => new { name = elementProvider.LocalName(attr), value = elementProvider.Value(attr) }).ToArray(); if (attrs.Any()) { index["a"] = attrs; } var elements = elementProvider.Elements(xelement).Select(child => CreateJsFromXElement(elementProvider, child)).Where(child => child != null).ToArray(); if (elements.Any()) { index["e"] = elements; } var texts = elementProvider.Texts(xelement).ToArray(); if (texts.Any()) { index["t"] = new { value = texts.JoinToString(null) } } ; var raws = elementProvider.Raws(xelement).ToArray(); if (raws.Any()) { index["h"] = raws.JoinToString(null); } return(index); } }