My Super Generic Webservice DataTable to serialisable XML string method call
OK I couldn’t hold back as to how super this C# method I wrote yesterday and would like you guys to know about.
Essentially what it does is that it reads in an SQL database tables, converts ii into a DataTable using SQL Data Adapter then the very clever bit is that it convert it to a MemorySteam that is XML serialisable over the HTTP to WCF Data Services for it to be received on the other end as a Stream and serialised back to DataTable again! Its fantastic, and its so generic, you can use this time and time again. Hope its useful for you!
public string Add(string webServiceCall, string sourceTable, string destinationTable) { // Restful service URL string url = readServiceUri().ToString() + webServiceCall;
// declare ascii encoding ASCIIEncoding encoding = new ASCIIEncoding(); string strResult = string.Empty; string xmlResult = "";
DataSet dataSet = new DataSet(); DataTable dataTable = new DataTable(); dataTable.TableName = destinationTable; using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString)) { SqlCommand cmd = new SqlCommand("SELECT * FROM " + sourceTable, conn);
cmd.CommandType = CommandType.Text; SqlDataAdapter adapter = new SqlDataAdapter(); try { adapter.SelectCommand = cmd; adapter.Fill(dataTable); } catch (Exception ex) { throw new Exception("ERROR " + ex); } }
// Important logical conversion to XML string dataSet.Tables.Add(dataTable); using (MemoryStream memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet)); xmlSerializer.Serialize(streamWriter, dataSet); xmlResult = Encoding.UTF8.GetString(memoryStream.ToArray()); } }
try {
string postData = xmlResult.ToString(); // Convert xmlstring to byte using ascii encoding byte[] data = encoding.GetBytes(postData); // Declare httpwebrequet wrt url defined above HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); // Set method as post webRequest.Method = "POST"; // Set content type webRequest.ContentType = "application/x-www-form-urlencoded"; // Set content length webRequest.ContentLength = data.Length; // Set timeout webRequest.Timeout = 60000; // Get stream data out of webrequest object Stream newStream = webRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); // Declare & read response from service HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); if (webResponse.StatusCode == HttpStatusCode.OK) { // set utf8 encoding Encoding enc = System.Text.Encoding.GetEncoding("utf-8"); // read response stream from response object StreamReader loResponseStream = new StreamReader(webResponse.GetResponseStream(), enc); // read string from stream data strResult = loResponseStream.ReadToEnd(); // close the stream object loResponseStream.Close(); // close the response object webResponse.Close(); // below steps remove unwanted data from response string strResult = strResult.Replace("</string>", "");
if (strResult.Contains("OK")) { return "OK"; } else { return "FAIL"; }
} else { return "FAIL"; } } catch (Exception ex) { throw new Exception("ERROR " + ex); } }
Leave a Comment