1

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.

2 Answers 2

2

I'm not sure why you want to convert it into XML, why not just parse directly into objects?

Use something like http://json2csharp.com/ to generate your classes. Then use the DataContractJsonDeserializer to read the Json into the objects. For an example of how to go about that bit, please see this MSDN article.

Edt again : Ah, I see you are already using Newtonsoft.JSON - that is a good alternative to the built-in deserialisation. You can use that to deserialise to a set of objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am now using the json feed and have now got it working.
0

ZombieSheep is correct in asking why you want to convert from JSON to XML. You should convert the response directory to a business object. From there, you could invoke a method to serialize it to XML, but I highly recommend you rethink your idea about converting to XML before obtaining a list.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.