Created
May 28, 2013 22:58
-
-
Save bhameyie/5666780 to your computer and use it in GitHub Desktop.
Datactontract serializer with json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UploadServceClient | |
{ | |
RestClient RestClient; | |
public UploadServceClient() | |
{ | |
RestClient = new RestClient("http://localhost/MyService"); | |
} | |
public MyResponseMessage CallMyEndpoint(MyRequestMessage myRequest) | |
{ | |
string body = HackyBodyCreation(myRequest); | |
var restRequest = new RestRequest(Method.POST); | |
restRequest.RequestFormat = DataFormat.Xml; | |
restRequest.Resource = "Myoperation"; | |
restRequest.AddParameter("application/xml", body, ParameterType.RequestBody); | |
var restResponse = RestClient.Execute<MyResponseMessage>(restRequest); | |
return restResponse.Data; | |
} | |
private string HackyBodyCreation(MyRequestMessage myRequest) | |
{ | |
var builder = new StringBuilder(); | |
var serializer = new DataContractSerializer(typeof(MyRequestMessage)); | |
var xmlWriter = XmlWriter.Create(builder); | |
serializer.WriteObject(xmlWriter,myRequest); | |
xmlWriter.Close(); | |
return builder.ToString().Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>",""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment