I have an asmx webservice(FoodWs.asmx) with a webmethod (RetrieveData) and its output data type is datatable.
[WebMethod]
public DataTable RetrieveData()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Prrice", typeof(int));
DataRow dr = dt.NewRow();
dr["Name"] = "Rice";
dr["Price"] = 1000;
dt.TableName = "Food";
dt.Rows.Add(dr);
return dt;
}
I'm calling this webservice dynamically with following code instead of adding web reference in another project.
public static HttpWebRequest CreateWebRequest()
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:55500/FoodWs.asmx?op=RetrieveData");
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
public static void Execute()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<RetrieveData xmlns=""http://tempuri.org/"" />
</soap:Body>
</soap:Envelope>");
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
**string soapResult = rd.ReadToEnd();**
HttpContext.Current.Response.Write(soapResult);
}
}
}
In the Execute method the result in soapResult variable is xml string of soap result. but in the webmethod of FoodWs.asmx the RetrieveData output datatype is datatable.
How can I convert the result to datatable?
How can I convert the XML SOAP result to its original datatype instead of XML string?