Showing posts with label Xml. Show all posts
Showing posts with label Xml. Show all posts

Friday, May 25, 2012

How to convert DataTable to XML in C#?

Following code illustrates about converting a DataTable to XML format. This is often required when passing a DataTable to a stored procedure. We can pass an XML directly to the procedure and process it.

private string ConvertDataTableToXML(DataTable dtBuildSQL)
{

  DataSet dsBuildSQL = new DataSet();

  StringBuilder sbSQL;
  StringWriter swSQL;
  string XMLformat;

  sbSQL = new StringBuilder();
  swSQL = new StringWriter(sbSQL);
  dsBuildSQL.Merge(dtBuildSQL, true, MissingSchemaAction.AddWithKey);
 dsBuildSQL.Tables[0].TableName = "Table";
 foreach (DataColumn col in dsBuildSQL.Tables[0].Columns)
 {
    col.ColumnMapping = MappingType.Attribute;
 }
 dsBuildSQL.WriteXml(swSQL);
 XMLformat = sbSQL.ToString();
 return XMLformat;
} 

Monday, December 12, 2011

How to send/receive SOAP request and response and return in dataset using C#

public Dataset GetCountries (string _agentCode, string _password)
       {
           WebRequest webRequest = WebRequest.Create("http://www.flexiblecarhire.com/flexibleservice/fchxmlinterface.asmx");
           HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
           httpRequest.Method = "POST";
           httpRequest.ContentType = "text/xml; charset=utf-8";
           httpRequest.Headers.Add("SOAPAction: http://fchhost.org/GetCountries");
           Stream requestStream = httpRequest.GetRequestStream();
           //Create Stream and Complete Request           
           StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);


           StringBuilder soapRequest = new StringBuilder("");
           soapRequest.Append("");
           soapRequest.Append("" + _agentCode + "");
           soapRequest.Append("" + _password + "");
           soapRequest.Append("");

           streamWriter.Write(soapRequest.ToString());
           streamWriter.Close();
           //Get the Response  
           HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
           StreamReader srd = new StreamReader(wr.GetResponseStream());
         
           DataSet ds = new DataSet();
           ds.ReadXml(wr.GetResponseStream());

           return ds;
       }