Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello , I am still beginner to Json, I want to know how to read a txt file contain json data like this below and how to convert it to xml data an present it.

here is my json data for example:
JavaScript
{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

I found many articles in the internet talking about that but no one of them worked with me like for example I an use a class called JsonReaderWriterFactory and many other methods and classes but I'm so confused don't know what to use .
Posted
Updated 6-Jan-15 0:57am
v2
Comments
KaushalJB 6-Jan-15 4:19am    
Have you referred this link : http://stackoverflow.com/questions/814001/convert-json-string-to-xml-or-xml-to-json-string
Hesham el Masry 6-Jan-15 4:50am    
yes I checked it but I cant find this class I type and its not working I don't know which library I should use for this class
KaushalJB 6-Jan-15 6:58am    
Download this and put a reference and use : http://json.codeplex.com/releases/view/84550

Add Json.NET library (use NuGet) to your project and use it for converting - can be both direction...
Open help[^] page of the library, on the left pane select Samples->Converting XML->Convert JSON to XML...
You will have a working sample there...
Home page of the library: http://james.newtonking.com/json[^]
 
Share this answer
 
after one day trying to do what I was searching for finally I finished and thanks to "KORNfel Eliyahu peter"
here is the solution :

FISRT:
if I have a json string and I want to read it :

my string:

C#
string json = "{\"employees\": [{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\" }]}";

I do parsing using json.net library 

 JObject o = JObject.Parse(json);

here if print it 

 Console.WriteLine(o["employees"]);


SECOND:

Converting from json string to XML:

MY JSON STRING :
C#
string json = @"{
   '?xml': {
     '@version': '1.0',
    '@standalone': 'no'
   },
   'root': {
     'person': [
       {
         '@id': '1',
        'name': 'Alan',
        'url': 'http://www.google.com'
      },
      {
'@id': '2',        'name': 'Louis',
        'url': 'http://www.yahoo.com'
     }    ]
  }}";


I use XNODE from
VB
System.Xml.Linq


XNode node = JsonConvert.DeserializeXNode(json,"root");

and here I converted the json string

finally I printed it :
Console.WriteLine(node.ToString());

don't forget to change it to "string" because anything appear on screen should be string.

HERE is the library and namespaces I used at top :

first I downloaded json.et library from : click on your assembly >
SQL
.select 'Manage Nuget Packages for Solution'
.click 'online', in the search box type 'newtonsoft'
.click 'Install' on 'Json.NET' (probably, what your looking for might be in another package, I'll ask you to track it down).



and then type these namespaces up :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.JsonResult;
using System.Xml.Linq;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900