2

i have kind of wired requirement for converting JSON to xml. we have an API that returns the JSON response as below.

{
   "status":"Error",
   "errorMessages":{
      "1001":"Schema validation Error"
   }
}

We want to convert this JSON to XML as below using c#

<root>
  <status>ERROR</status>
  <errorMessages>
    <ErrorCode>1001</ErrorCode>
    <ErrorDescription>Schema validation Error</ErrorDescription>    
  </errorMessages>
</root>

The API team is very resistant to change the way there are generating the JSON. So i have to find a way to convert this json to XML.

I am getting the below error when i try to convert

XmlDocument doc = JsonConvert.DeserializeXmlNode(json);

"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 errorMessages

thanks for the help in advance. :)

2
  • when i try to run the deserializeXmlNode method i am getting the below error XmlDocument doc = JsonConvert.DeserializeXmlNode(json); ` i am getting the below error when i try to convert "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 'errorMessages ` Commented Mar 21, 2018 at 12:44
  • You need to write a custom transformer for this JSON because 1) it has no root node (as the error says) and 2) it has properties that begin with a number (which would be invalid XML element names). There is no direct conversion path. Commented Mar 21, 2018 at 13:19

4 Answers 4

2

At first, I would hardly recommend that your API team provides a valid JSON object. Otherwise you will have to write a converter that does the job. The converter could look like this:

using System.Collections.Generic;

namespace Json
{
    using System.IO;
    using System.Xml.Serialization;

    using Newtonsoft.Json.Linq;

    class Program
    {
        static void Main(string[] args)
        {
            var converter = new Converter();
            converter.Convert();
        }
    }

    class Converter
    {
        public void Convert()
        {
            //  your JSON string goes here
            var jsonString = @"{""status"":""Error"",""errorMessages"":{ ""1001"":""Schema validation Error"", ""1953"":""Another error""}}";

            // deconstruct the JSON
            var jObject = JObject.Parse(jsonString);
            var root = new Root { Status = jObject["status"].ToString(), ErrorMessages = new List<ErrorMessage>() };
            foreach (var errorMessageJsonObject in jObject["errorMessages"])
            {
                var jProperty = (JProperty)errorMessageJsonObject;
                var errorCode = System.Convert.ToInt16(jProperty.Name);
                var errorDescription = jProperty.Value.ToString();
                var errorMessage = new ErrorMessage() { ErrorCode = errorCode, ErrorDescription = errorDescription};

                root.ErrorMessages.Add(errorMessage);
            }

            // serialize as XML
            var xmlSerializer = new XmlSerializer(typeof(Root));
            string xml;
            using (StringWriter textWriter = new StringWriter())
            {
                xmlSerializer.Serialize(textWriter, root);
                xml = textWriter.ToString();
            }
        }
    }

    public class Root
    {
        public string Status;

        public List<ErrorMessage> ErrorMessages;
    }

    public class ErrorMessage
    {
        public int ErrorCode;

        public string ErrorDescription;
    }
}

With this, you will read the JSON, deconstruct it into a proper object and serialize it as XML.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot @mu88 this worked. :) we have asked out API team to change it but we are not sure if that will be doing it ..
Great! If you like this post, please give it your vote and/or mark it as an answer.
1

if you are using asp.net web api. Its already can return xml response just add an accept header like

Accept: application/xml

3 Comments

when i add the above header its will fail as the xml element cannot start with an integer. <root> <status>ERROR</status> <errorMessages> <1274>Schema validation Error</1274> </errorMessages> </root>
i am consuming this service in biztalk server. So i need to write a custom c# component that does this conversion.
1

From the documentation at https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm

string json = @"{
  '?xml': {
    '@version': '1.0',
    '@standalone': 'no'
  },
  'root': {
    'person': [
      {
        '@id': '1',
        'name': 'Alan',
        'url': 'http://www.google.com'
      },
      {
        '@id': '2',
        'name': 'Louis',
        'url': 'http://www.yahoo.com'
      }
    ]
  }
}";

XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);

Comments

0

Use the JsonConvert class which contains helper methods for this precise purpose:

// To convert an XML node contained in string xml into a JSON string   
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);

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

The Completed Documentation : Here

2 Comments

hi carlos thanks for the response. i am getting the below error when i try to convert "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 'errorMessages'"
Hi Abhi pm, try it if possible for you. link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.