I've run into some trouble trying to send a multi-content POST from a .NET application to another .NET application. In this particular example, I am receiving an XML document in a string, and then passing that onto a different .NET application so it can parse it. I can see the other application is receiving the XML, but the application seems to read it as containing a quote at the beginning and end like a string which is causing the parse to break as it isn't valid XML.
I'm trying not to change this parsing service as a different, currently working usage involves uploading an XML Document which the service is then passing through to the parsing application. This is working as expected, so I'm basically trying to make the first service pass information in a similar way.
The below code is my current non-working code
var fileContent = "<xml stuff></xml stuff>"
var multiContent = new MultipartFormDataContent();
var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent));
var streamContent = new StreamContent(memoryStream);
streamContent.Headers.Remove("Content-Type");
streamContent.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/xml");
multiContent.Add(streamContent, "file", "toBeParsed.xml");
multiContent.Add(new StringContent(applicationData, Encoding.UTF8, "application/json"));
return multiContent;
I adapted this code from the below working example which directly passes on a supplied file
var multiContent = new MultipartFormDataContent();
var streamContent = new StreamContent(files.First().OpenReadStream());
streamContent.Headers.Remove("Content-Type");
streamContent.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/xml");
multiContent.Add(streamContent, "file", "toBeParsed.xml");
multiContent.Add(new StringContent(applicationData, Encoding.UTF8, "application/json"));
return multiContent;
Any tips to correct this?