0

I'm a relatively new programmer and therefore have limited knowledge; however, I've been asked to create a program to convert loads of json files to xml files. There are a lot of them, and they're all different in terms of content (and i don't know exactly what's in them).

I've tried the following code

static void ProcessFiles(string path)
    {
        string[] files;
        string[] directories;

        XmlDocument xml = new XmlDocument();
        files = Directory.GetFiles(path);
        foreach (string file in files)
        {
            using (StreamReader r = new StreamReader(file))
            {
                string j = r.ReadToEnd();
                string json = JsonConvert.DeserializeObject(j).ToString();
                xml = JsonConvert.DeserializeXmlNode(json);

                Console.Write(xml);
            }
        }

        directories = Directory.GetDirectories(path);
        foreach(string directory in directories)
        {                
            ProcessFiles(directory);
        }
    }

I've managed to get this as my string 'json' and then get an error.

    [
  {
    "Start": "date",
    "Finish": "date",
    "Subject": "",
    "Comments": "",
    "Site": "address",
    "Location": null,
    "Status": false,
    "Arrived": true,
    "Noshow": false,
    "Services": "Initial Consultation",
    "Attendees": [
      {
        "AccountId": 1111,
        "AccountType": "MP",
        "Name": "MMS (FP), Support "
      },
      {
        "AccountId": 2220915,
        "AccountType": "PA",
        "Name": "Test, Patient "
      }
    ]
  },
]

I've been looking online for a solution but no luck so far. Can anyone help please?

  • 4
    Possible duplicate of [How to convert JSON to XML or XML to JSON?](https://stackoverflow.com/questions/814001/how-to-convert-json-to-xml-or-xml-to-json) – Fourat Mar 21 '19 at 10:57
  • The LoadXml() method expects the input to be XML. Right now you are taking json and creating an object that is not xml and then trying to parse as if it is xml. You need to take the deserialize json and convert to xml. – jdweng Mar 21 '19 at 11:09
  • Converting the files to "any old XML" is pointless; someone must have a view on who is going to consume the XML, and that consumer will have specific requirements about the XML vocabulary/schema that's expected. So there's a requirement here that you're missing, and it has a fundamental bearing on how you tackle the conversion. – Michael Kay Mar 21 '19 at 11:50
  • The consumer is me. i need to convert the files 'as is' from json to xml so i can then do something else. – Sam Boniface Mar 21 '19 at 12:14

2 Answers2

1

You are close to your goal,

So now you are doing like,

  1. Read string json from StreamReader with ReadToEnd
  2. And then deserialized into dynamic variable.
  3. Then load into xml.

But at point 3 you are trying to pass json string to LoadXml method where as LoadXml want xml string as input.

So far, newtonsoft have one method that can directly convert your json to xml

XmlDocument xdoc = JsonConvert.DeserializeXmlNode(json);

So now your code look like,

//Your code as it is

using (StreamReader r = new StreamReader(file))
{
    string json = r.ReadToEnd();
    xdoc = JsonConvert.DeserializeXmlNode(json);

    xdoc.Save(file + ".xml");
}

//Your code as it is
er-sho
  • 9,581
  • 2
  • 13
  • 26
  • Thank you for coming back to me. I'm now getting this error: "Newtonsoft.Json.JsonSerializationException: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path 'Name', line 1, position 29.'" – Sam Boniface Mar 21 '19 at 11:30
  • Yes that's because of your json have multiple properties at root and `DeserializeXmlNode` work with single root property. let me update my answer with your need – er-sho Mar 21 '19 at 11:43
  • How do you get it to work with multiple properties if you don't know what they are? – Sam Boniface Mar 21 '19 at 12:52
0

It seems to me that you just need this:

static void ProcessFiles(string path)
{
    foreach (string file in Directory.GetFiles(path))
    {
        JsonConvert.DeserializeXmlNode(File.ReadAllText(file)).Save(file + ".xml");
    }

    foreach (string directory in Directory.GetDirectories(path))
    {
        ProcessFiles(directory);
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Thank you for coming back to me. I'm now getting this error: "Newtonsoft.Json.JsonSerializationException: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path 'Name', line 1, position 29.'" – Sam Boniface Mar 21 '19 at 11:32
  • @SamBoniface - You might need to have posted samples of the JSON files you're converting. – Enigmativity Mar 21 '19 at 12:05