Parsing JSON into XML using Windows Phone

Posted by Henry Edwards on Stack Overflow See other posts from Stack Overflow or by Henry Edwards
Published on 2012-01-30T12:56:29Z Indexed on 2012/03/25 17:29 UTC
Read the original article Hit count: 423

I have this code, but can't get it all working.

I am trying to get a json string into xml. So that I can get a list of items when i parse the data. Is there a better way to parse json into xml.

If so what's the best way to do it, and if possible could you give me a working example?

The URL that is in the code is not the URL that i am using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Bson;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Xml.Linq.XDocument;
using System.IO;



namespace WindowsPhonePanoramaApplication3
{
public partial class Page2 : PhoneApplicationPage
{
    public Page2()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e1)
    {
        /* because the origional JSON string has multiple root's this needs to be added */
        string json = "{BFBC2_GlobalStats:";
        json += DownlodUrl("http://api.bfbcs.com/api/xbox360?globalstats");
        json += "}";

        XmlDocument doc = (XmlDocument)JsonConvert.DeserializeObject(json);
        textBox1.Text = GetXmlString(doc);
    }

    private string GetXmlString()
    {
        throw new NotImplementedException();
    }


    private string DownlodUrl(string url)
    {
        string result = null;
        try
        {
            WebClient client = new WebClient();
            result = client.DownloadString(url);
        }
        catch (Exception ex)
        {
            // handle error
            result = ex.Message;
        }
        return result;
    }

    private string GetXmlString(XmlDocument xmlDoc)
    {
        sw = new StringWriter();
        XmlTextWriter xw = new XmlTextWriter(sw);
        xw.Formatting = System.Xml.Formatting.Indented;
        xmlDoc.WriteTo(xw);
        return sw.ToString();
    }

        }
}

The URL outputs the following code:

{"StopName":"Race Hill",
"stopId":7553,
"NaptanCode":"bridwja",
"LongName":"Race Hill",
"OperatorsCode1":" 5",
"OperatorsCode2":" ",
"OperatorsCode3":" ",
"OperatorsCode4":"bridwja",
"Departures":[
{
"ServiceName":"",
"Destination":"",
"DepartureTimeAsString":"",
"DepartureTime":"30/01/2012 00:00:00",
"Notes":""}`

Thanks for your responses. So Should i just leave the data a json and then view the data via that???

Is this a way to show the data from a json string.

public void Load()
{
// form the URI
UriBuilder uri = new UriBuilder("http://mysite.com/events.json");
WebClient proxy = new WebClient();  
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(OnReadCompleted);  
proxy.OpenReadAsync(uri.Uri);  
}

void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
var serializer = new DataContractJsonSerializer(typeof(EventList));
    var events = (EventList)serializer.ReadObject(e.Result);
    foreach (var ev in events)
{
Items.Add(ev);
}
}
}

public ObservableCollection<EventDetails> Items { get; private set; }

Edit: Have now kept the url as json and have now got it working by using the json way.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about JSON