Converting a JSON object to XML to be passed to an ASMX webservice What I have is a scenario where we need to be able to call any ASMX web service and pass any data structure to it that has been constructed on the JavaScript side.

Until now, I have been creating service references for ASMX web services in order to call them, but in this scenario, I can no longer do that. This is my first go at passing XML to service references manually.

What I am not too sure of is the level of complexity required to get a JSON string into basically an XML string to be passed to the webservice.

 string func_name = "//some asmx web service function"
 string json_string = "//some json encoded string here";
 XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(variables, "root");
 StringWriter stringWriter = new StringWriter();
 XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
 doc.WriteTo(xmlTextWriter);
 string xml_parameters = stringWriter.ToString();

 //remove &lt;root&gt; and &lt;/root&gt; from the string
 xml_parameters = xml_parameters.Replace("&lt;root&gt;", null).Replace("&lt;/root&gt;", null);

string request_xml= "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;" +
                            "&lt;soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"&gt;" +
                               "&lt;soap:Body&gt;" +
                                   "&lt;" + func_name + " xmlns=\"http://tempurl.org\" &gt;" +
                                     xml_parameters+
                                   "&lt;/" + func_name +" &gt;" +
                               "&lt;/soap:Body&gt;" +
                           "&lt;/soap:Envelope&gt;";

HttpResponseMessage response = client.PostAsync(uri, new StringContent(xml, Encoding.UTF8, request_header_type)).Result;


An awful lot of objects have been instantiated here in order to get my JSON converted into XML to be passed to the web service. Can anyone suggest any improvements or potential problems?
Use camelCase to name local variables instead of words separated by underscores. Although there are no guidelines for them it is common practice among C# programmers.

You should always call XmlWriter.Create to get XmlWriter instead of creating XmlTextWriter.

StringWriter, XmlWriter and any other writer implement IDisposable and thus should be used within using section:

using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
    // ... do your stuff that requires stringWriter and xmlTextWriter
}


You should try to avoid .Result for async operation. Try to mark your method with async and use await for client.PostAsync.

Creation of XML looks awful... I suggest to extract your request XML template into XML document with kind of placeholders like:

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"&gt;
    &lt;soap:Body&gt;
        &lt;FUNC_NAME xmlns="http://tempurl.org"&gt;
            XML_PARAMETERS
        &lt;/FUNC_NAME&gt;
    &lt;/soap:Body&gt;
&lt;/soap:Envelope&gt;


Then in your code just read this file to string with File.ReadAllText and replace FUNC_NAME and XML_PARAMETERS with string.Replace with real values.