locked
Xml to Json conversion RRS feed

  • Question

  •  hi ,

    can any one help me out with this,

    I have a xml response , i need to convert that to json .

    Thanx in advance,

    Hari.
     

    Saturday, June 14, 2008 11:45 AM

Answers

  • OK, I got what you mean. After you've got the xml content from your SOAP message by invoking methods of the generated proxy class, you can do this:

    XDocument doc = XDocument.Parse(theXmlContent);
    XElement root = doc.Root;
    var query = from folder in root.Descendants("folder")
    select new folder()
    {
    view = (string)folder.Attribute("view"),
    n = (int)folder.Attribute("n"),
    l = (int)folder.Attribute("l"),
    name = (string)folder.Attribute("name"),
    s = (int)folder.Attribute("s"),
    id = (int)folder.Attribute("id"),
    rev = (int)folder.Attribute("rev")
    };
    List<folder> folders = query.ToList();
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<folder>));
    MemoryStream ms = new MemoryStream();
    serializer.WriteObject(ms, folders);
    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);
    string json = sr.ReadToEnd();
    sr.Close();
    ms.Close();

    public class folder
    {
    public string view { get; set; }
    public int n { get; set; }
    public int l { get; set; }
    public string name { get; set; }
    public int s { get; set; }
    public int id { get; set; }
    public int rev { get; set; }
    }

    Tuesday, June 17, 2008 12:54 AM

All replies

  • An Xml response from WHAT? And what does it look like? More information, better answers.

    Saturday, June 14, 2008 3:02 PM
  • I have a xml response , i need to convert that to json .
     

    You can google it. XML to JSON 

    Sunday, June 15, 2008 10:11 PM
  • OK, I got what you mean. After you've got the xml content from your SOAP message by invoking methods of the generated proxy class, you can do this:

    XDocument doc = XDocument.Parse(theXmlContent);
    XElement root = doc.Root;
    var query = from folder in root.Descendants("folder")
    select new folder()
    {
    view = (string)folder.Attribute("view"),
    n = (int)folder.Attribute("n"),
    l = (int)folder.Attribute("l"),
    name = (string)folder.Attribute("name"),
    s = (int)folder.Attribute("s"),
    id = (int)folder.Attribute("id"),
    rev = (int)folder.Attribute("rev")
    };
    List<folder> folders = query.ToList();
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<folder>));
    MemoryStream ms = new MemoryStream();
    serializer.WriteObject(ms, folders);
    StreamReader sr = new StreamReader(ms);
    ms.Seek(0, SeekOrigin.Begin);
    string json = sr.ReadToEnd();
    sr.Close();
    ms.Close();

    public class folder
    {
    public string view { get; set; }
    public int n { get; set; }
    public int l { get; set; }
    public string name { get; set; }
    public int s { get; set; }
    public int id { get; set; }
    public int rev { get; set; }
    }

    Tuesday, June 17, 2008 12:54 AM
  •  hmm got it dude ,

    thanx a lot ,

    now after getting a json object, do i need to do the same to get the content out of the json .

     

    Tuesday, June 17, 2008 4:02 AM