Get XML Response RRS feed

  • Question

  • User-1446158388 posted

    I am using PostAsync() and it works to perfection to actually Post() the data.  My issue is that I need to GET() an XML response from the page showing if it was valid or invalid.  If it is valid I get a response of


    if it is invalid - I get a response of 

    <?xml version="1.0" encoding="utf-8" ?>
        <error>This is what caused the error</error>

    And this is how I am actually using the POSTASYNC() -

    private void MultiPosts()
      string connectionnectionString = "dtableta Source=hmod;Initial Catalog=salesdb;uid=userid;password=password;MultipleActiveResultSets=True";
      DataTable gridfromdtableta = new DataTable();
      using (var connection = new Sqlconnectionnection(connectionnectionString))
      using (var command = new SqlCommand("Pulldtableta", connection))
      using (var dtable = new SqldtabletaAdtablepter(command))
        command.CommandType = CommandType.StoredProcedure;
      var client = new HttpClient();
      var q = string.Empty;
      foreach (dtabletaRow row in gridfromdtableta.Rows)
        var postdtableta = new List<KeyValuePair<string, string>>();
        for (int i = 0; i < gridfromdtableta.Columns.Count; ++i)
            postdtableta.Add(new KeyValuePair<string, string>(gridfromdtableta.Columns[i].ColumnName, row[i].ToString()));
        var result = client.PostAsync("http://submitthis/ashx", new FormUrlEncodedconnectiontent(postdtableta)).Result;

    What do I need to alter in order to read the xml response from each individual POST()

    Tuesday, March 29, 2016 1:47 PM


  • User36583972 posted

    Hi ShowFreeFlying,

    As far as I know, you can try the following steps.

    Firstly, you should define Web API return MediaType is XmlMediaTypeFormatter.


                GlobalConfiguration.Configuration.Formatters.Add(new System.Net.Http.Formatting.XmlMediaTypeFormatter());

    Secondly, define a custom return response.

     public HttpResponseMessage GetProductTest(int id)
                if (id == 0)
                    ErrorResponse errors = new ErrorResponse();
                    errors.error = "This is what caused the error";
                    errors.success = "1";
                    var response = Request.CreateResponse(HttpStatusCode.OK, errors);
                    return response;
                    SuccessResponse success = new SuccessResponse();
                    success.success = "0";
                    var response = Request.CreateResponse(HttpStatusCode.OK, success);
                    return response;
     public class ErrorResponse
            public string error { get; set; }
            public string success { get; set; }
        public class SuccessResponse
            public string success { get; set; }

    The following tutorial for your reference.

    Outputting custom xml with .net web api:


    Handling Exceptions in ASP.NET Web API:


    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 30, 2016 4:38 AM