Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,


Please guide me the how to convert a json file to user defined xml format in asp.net.
Posted

1 solution

Parse JSON into some data structure and serialize it as XML. Note that the problem is ambiguous, the solutions can be different (not just the methods of solutions, but the results).

The best method would be using Data Contract, but this possibility depends on what is your given JSON schema (in case you cannot change it). Please see:
https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx[^] (JSON),
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer%28v=vs.110%29.aspx[^] (XML).

Other possibilities a reduced to your own parsing and unparsing. On JSON part, please see my past answers:
haw to get data from Cloudant (json document)[^],
how to conver multi level json data to C# Object?[^],
How To Convert object type to C# class object type[^].

On XML part, .NET FCL offers several approaches. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.


Good luck,
—SA
 
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