XML -> JSON Serialization

With the next release of Ajax.NET Professional you will be able to transform any XmlDocument or XmlNode into a JSON object. This is really cool because we have already a lot of xml documents in our web applications. Currently you have to write a custom class or struct where you have to fill the properties or public fields. Now, you simply call the JavaScriptUtil.GetIJavaScriptObjectFromXmlNode method to return a JavaScript object. At http://munich.schwarz-interactive.de/datatype.aspx I have added a new test (Test 32) which will read the RSS from the Google group and display the title and last 10 posts.

My C# server-side source code looks like this:

[AjaxPro.AjaxMethod]
public static AjaxPro.IJavaScriptObject Test31()
{
    XmlDocument doc = new XmlDocument();
    doc.Load("
http://groups.google.de/group/ajaxpro/feed/rss_v2_0_msgs.xml");
    return AjaxPro.JavaScriptUtil.GetIJavaScriptObjectFromXmlNode(doc.DocumentElement);
}

The RSS xml document looks like this:

<rss version="2.0">
    <channel>
        <title>Ajax.NET Professional Google Group</title>
        <link>http://groups.google.com/group/ajaxpro</link>
        <item>
            <title>Re: [ajaxpro] Title of post</title>
            // ...
        </item>
        <item>
            // ...
        </item>
    </channel>
</rss>

Now, you are able to write JavaScript code like this:

<script type="text/javascript">

function callback(res) {
    var chn = res.value.channel;
    alert("The RSS channel title: " + chn.title + "\r\nLink: " + chn.link);

    var h = [];

    for(var i=0; i<chn.item.length; i++) {
        h.push(chn.item[i].title + "\r\n");
    }

    alert(h.join(""));
}

</script>

No Comments