Convert JSON to SOAP XML with predefined namespaces

Viewed 35

I am given task to convert SOAP request to ReST JSON and then JSON back to SOAP XML. I have done the part of converting SOAP to JSON with Serialize and XMlElement() classes. Now the trickiest part comes where I need to convert JSON to SOAP. There are our own namespaces which are written and I want to put those namespaces back as well when converting JSON to SOAP XML. For example,

<asr:APIRequest

xmlns:guid-"http://Some-test-namespace.xsd" xmlns:xsi="https://www.w3.org/2001/XMLSchema" xmins:asw="http://Some-test-namespace.xsd" xmIns:ar="http://Some-test-namespace.xsd" xmlns:asr="http://Some-test-namespace.xsd">

<asr:APIRequestDetails>

<ar:Individual>

<ar:IndividualKey>3791239123-123123123-1231231</ar:IndividualKey>

</ar:Individual> 

</asr:APIRequestDetails> 
</asr:APIRequest>

I converted it to JSON

{
  "APIRequest": {
    "APIRequestDetails": {
      "Individual": {
        "IndividualKey": "3791239123-123123123-1231231"
      }
    }
  }
}

Now I want to convert it back to SOAP XML with the namespaces that are attached such as 'asr', 'ar'. I am using C# .Net Core and used XDocument, XMLSerializer for converting XML to JSON.

can anyone please help me converting JSON back to SOAP XML with the NAMESPACES as well?

1 Answers

Namespace do not get added unless used. Try following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication40
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(INPUT_FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(APIRequest));
            APIRequest request = (APIRequest)serializer.Deserialize(reader);

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("guid", "http://Some-test-namespace.xsd");
            namespaces.Add("xsi", "https://www.w3.org/2001/XMLSchema");
            namespaces.Add("asw", "http://Some-test-namespace.xsd");
            namespaces.Add("ar", "http://Some-test-namespace.xsd");
            namespaces.Add("asr", "http://Some-test-namespace.xsd");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME,settings);
            serializer.Serialize(writer, request, namespaces);
        }

    }
    [XmlRoot(Namespace = "http://Some-test-namespace.xsd")]
    public class APIRequest
    {
        [XmlElement(Namespace = "http://Some-test-namespace.xsd")]
        public APIRequestDetails APIRequestDetails { get; set; }
    }
    public class APIRequestDetails
    {
        [XmlElement(Namespace = "http://Some-test-namespace.xsd")]
        public Individual Individual { get; set; }
    }
    public class Individual
    {
        [XmlElement(Namespace = "http://Some-test-namespace.xsd")]
        public string IndividualKey { get; set; }
    }
}
Related