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>
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.
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().
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 .
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With