Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to XML Conversion in C#

Tags:

json

c#

json.net

I've been using Json.Net to parse JSON to object and convert to XMLDocument but I got

InvalidOperationException This document already has a 'DocumentElement' node.

I have this JSON data:

{
   "data": [
      {
         "name": "Eros Harem",
         "id": "2345123465"
      },      
      {
         "name": "Vincent Dagpin",
         "id": "56783567245"
      },
      {
         "name": "Vrynxzent Kamote",
         "id": "3456824567"
      }
   ],
   "paging": {
      "next": "nextURLHere"
   }
}

and this is my code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using Newtonsoft.Json;

namespace JsonToXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = File.ReadAllText("friends.json");

            // To convert JSON text contained in string json into an XML node
            XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
        }
    }
}

did i miss some settings?

I expect to have something like this as output.

<?xml version="1.0"?>
<friends>    
    <data>
        <name>Eros Harem</name>
        <id>2345123465</id>        
    <data>
        <name>Vincent Dagpin</name>
        <id>56783567245</id>        
    </data>
    <data>
        <name>Vrynxzent Kamote</name>
        <id>3456824567</id>        
    </data>
    <paging>
        <next>nextURLHere</next>
    </paging>
</friends>
like image 561
Vincent Dagpin Avatar asked Aug 02 '12 03:08

Vincent Dagpin


People also ask

Can you convert JSON to XML?

To convert a JSON document to XML, follow these steps: Select the JSON to XML action from the Tools > JSON Tools menu. Choose or enter the Input URL of the JSON document. Choose the path of the Output file that will contain the resulting XML document.

Can we convert JSON to XML in C#?

Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter. The JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode().

How do I change the language from JSON to Notepad ++?

You have to use the plugin manager of Notepad++ and search for the JSON plugin. There you can easily install it. This answer explains it pretty good: How to reformat JSON in Notepad++? On their website sunjw.us/jstoolnpp/screenshots.php it's listen as JSMin .

How do I export JSON to excel?

On the “Data” tab, from the “Get & Transform Data” section, select Get Data > From File > From JSON. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.


1 Answers

What you need is a root element in your json I think. which is what XML needs.

which I think you can do by

XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json, "friends");
like image 85
Keith Nicholas Avatar answered Sep 27 '22 21:09

Keith Nicholas