Thursday, March 7, 2013

Convert XML string into DataTable object


 public DataTable ConvertXMLIntoDataTable(string inputData)
        {
            DataTable dtDataConverted = null;
            try
            {              
                StringReader stringReader = new StringReader(inputData);
                DataSet dsData = new DataSet();
                dsData.ReadXml(stringReader);
                dtDataConverted = dsData.Tables[0];
            }
            catch (Exception ex)
            {  }
            return dtDataConverted;
        }

Convert Datatable object into XElement object


public XElement ConvertDataTableIntoXElement(DataTable dtData)
        {
            XElement xElementConverted = null;
            try
            {
                xElementConverted = new XElement("container");
                using (XmlWriter xmlWriter = xElementConverted.CreateWriter())
                { dtData.WriteXml(xmlWriter, System.Data.XmlWriteMode.WriteSchema, true); }
            }
            catch (Exception ex)
            {  }
            return xElementConverted;
        }

Wednesday, March 6, 2013

Basic RESTful service in WCF

operation contract in the interface for the WCF service

[OperationContract]
        [WebInvoke(Method = "GET",
            BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml,
            UriTemplate = "/ValidateXMLData?inputData={inputData}")]
        string ValidateXMLData(string inputData);

In the .svc file do the implementation for the interface.

   public string ValidateXMLData(string inputData)
        {
            string validatedString=string.Empty;
            try
            {
                 XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(inputData);
                validatedString = inputData;
            }
            catch (Exception ex)
            { }
            return validatedString;
        }

service configuration in your web.Config file

<system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="MyService.Services.RestService">
        <endpoint address="XML"
                  behaviorConfiguration="restPoxBehavior"
                  binding="webHttpBinding"
                  contract="MyService.Contracts.IRestService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Motivational qoutes

पूरे विश्वास के साथ अपने सपनों की तरफ बढ़ें। वही ज़िन्दगी जियें जिसकी कल्पना आपने की है।