Last active
March 16, 2016 10:19
-
-
Save leekelleher/ef052edb72cf2f698b20 to your computer and use it in GitHub Desktop.
Umbraco: Converting and namespacing the JSON data to XML (for use with XSLT/XPath)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<root id="-1"> | |
<HomePage id="1234" parentID="-1" level="1" creatorID="1" sortOrder="0" createDate="2014-12-16T00:00:00" updateDate="2014-12-16T00:00:00" nodeName="Home" urlName="home" path="-1,1156" isDoc="" nodeType="1111" creatorName="admin" writerName="admin" writerID="1" template="0" nodeTypeAlias="HomePage"> | |
<archetypeLinks><![CDATA[{"fieldsets":[{"properties":[{"alias":"linkUrl","value":"http://www.example.com/"},{"alias":"linkLabel","value":"Website"}],"alias":"externalLink","disabled":false},{"properties":[{"alias":"linkUrl","value":"http://www.example2.com/"},{"alias":"linkLabel","value":"Webshop"}],"alias":"externalLink","disabled":false}]}]]></archetypeLinks> | |
<json:archetypeLinks xmlns:json="http://pimpmyxslt.com/json"> | |
<fieldsets> | |
<properties> | |
<alias>linkUrl</alias> | |
<value>http://www.example.com/</value> | |
</properties> | |
<properties> | |
<alias>linkLabel</alias> | |
<value>Website</value> | |
</properties> | |
<alias>externalLink</alias> | |
<disabled>false</disabled> | |
</fieldsets> | |
<fieldsets> | |
<properties> | |
<alias>linkUrl</alias> | |
<value>http://www.example2.com/</value> | |
</properties> | |
<properties> | |
<alias>linkLabel</alias> | |
<value>Webshop</value> | |
</properties> | |
<alias>externalLink</alias> | |
<disabled>false</disabled> | |
</fieldsets> | |
</json:archetypeLinks> | |
</HomePage> | |
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dumping this on GitHub gist, for reference. (Else I'll probably lose it) [LK] | |
public class JsonXmlXsltEventHandler : ApplicationEventHandler | |
{ | |
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) | |
{ | |
umbraco.content.AfterContentCacheLoadNodeFromDatabase += (sender, e) => | |
{ | |
if (!sender.HasChildNodes) | |
return; | |
foreach (System.Xml.XmlNode item in sender.ChildNodes) | |
{ | |
if (item.NodeType == System.Xml.XmlNodeType.Element) | |
{ | |
if (item.HasChildNodes) | |
{ | |
var inner = item.FirstChild; | |
if (inner.NodeType == System.Xml.XmlNodeType.CDATA) | |
{ | |
var text = inner.InnerText; | |
if (text.DetectIsJson()) | |
{ | |
if (text.StartsWith("[")) | |
{ | |
text = "{\"arrayitem\":" + text + "}"; | |
} | |
try | |
{ | |
var jsonXml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(text, item.Name, true); | |
var newElement = item.OwnerDocument.CreateElement("json", item.Name, "http://pimpmyxslt.com/json"); | |
newElement.InnerXml = jsonXml.DocumentElement.InnerXml; | |
var newChild = item.OwnerDocument.ImportNode(newElement, true); | |
item.ParentNode.InsertAfter(newChild, item); | |
} | |
catch { /* do nothing */ } | |
} | |
} | |
} | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would this work from the
App_Code
folder if one were to add the necessary@using
statements?