My main objective is to convert a json file to CSV format with some formatting.
For that I have converted the json to xml.
Now, I want to populate the nested XML to a datagridview and then further process some formatting commands.
The issue is that one parent table and child table of the converted XML file are having the same name 'item'.
Please help me resolve the issue. The primary goal is to convert the file as is done by the website https://json-csv.com
I do not prefer using any third party reference. Please let me know if it can done using System.Runtime.Serialization.Json
The error received by me is
The table (item) cannot be the child table to itself in nested relations.
My code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.Serialization;
- using System.Xml; using System.Xml.Linq;
- using System.ServiceModel.Web;
- using System.Runtime.Serialization.Json;
- using System.Xml.XPath;
- namespace JsonToCsv
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- string Text = System.IO.File.ReadAllText(@"C:\0.json");
- var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(Text), new System.Xml.XmlDictionaryReaderQuotas());
- var root = XElement.Load(jsonReader);
- DataSet ds = new DataSet();
- System.IO.StringReader Reader = new System.IO.StringReader(root.ToString());
- richTextBox1.Text = root.ToString();
- ds.ReadXml(Reader);
- DataTable dt = new DataTable();
- ds.Tables.Add(dt);
- dataGridView1.DataSource = dt;
- } } }
The JSON file, the XML generated by the program and the output required are attached