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);
 string xml_parameters = stringWriter.ToString();

 //remove <root> and </root> from the string
 xml_parameters = xml_parameters.Replace("<root>", null).Replace("</root>", null);

string request_xml= "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                            "<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\">" +
                               "<soap:Body>" +
                                   "<" + func_name + " xmlns=\"http://tempurl.org\" >" +
                                   "</" + func_name +" >" +
                               "</soap:Body>" +

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?


1 Answer 1


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:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/" 
        <FUNC_NAME xmlns="http://tempurl.org">

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.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.