-1

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?

Makki Anjum
  • 138
  • 3
  • 10
  • You OBVIOUSLY can't as JSON doesn't contain this informations. – Selvin Sep 21 '22 at 10:43
  • wouldn't be there any method to do this? any tweaking? – Makki Anjum Sep 21 '22 at 10:44
  • ok ... An example: we convert list {2, 3, 4} to list {4, 3, 2} ... now we can't get {2, 3, 4} from {4, 3, 2} again – Selvin Sep 21 '22 at 10:45
  • don't say it is impossible then. I asked to get the answers, not to listen "it's impossible" – Makki Anjum Sep 21 '22 at 10:47
  • You didn't wrote that you have models ... so I thought that you need a generic solution for [any JSON conversion to XML](https://dotnetfiddle.net/Khfg5k) - which is possible but without namespaces (and array element names)... **but JSON in your question seems doesn't matter at all and all you asking is how to serialize model to xml with namespaces** – Selvin Sep 21 '22 at 15:00
  • ... which simply make this question a duplicate of https://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes – Selvin Sep 21 '22 at 15:06

1 Answers1

0

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; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • This doesn't answer the question which is how to convert given JSON back SOAP XML... Also OP didn't mention that he has model classes – Selvin Sep 21 '22 at 11:13
  • I do have Model classes. – Makki Anjum Sep 21 '22 at 11:31
  • @Selvin can you please stay away from this question. I don't need your answer. – Makki Anjum Sep 21 '22 at 11:33
  • @Selvin : Next time read the posting completely. The OP said he already was using xml serialization in the process. the OP just said he was having issues with the namespaces. – jdweng Sep 21 '22 at 11:43
  • he wrote about XDocument... So it could be free form XML. – Selvin Sep 21 '22 at 11:47
  • @Selvin : Posting didn't say to stop using XDocument. My posting did answer the question. Sevin you are answering a completely different question. – jdweng Sep 21 '22 at 12:22