Monday, August 19, 2013

Create webservice to get custom xml structure

* Add below line of code to your service.cs , used three tier architecture and Commented out direct db call .


#region HeaderOfXml
    private StringBuilder CreateBaseStringBuilder()
    {
        StringBuilder BaseDocument = new StringBuilder();
        BaseDocument.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        return (BaseDocument);
    }
    #endregion



    [WebMethod]
    public XmlDocument GetDealerList()
     {
         //  DataSet ds = new DataSet();
          //  SqlConnection cnn = new //SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
    //    cnn.Open();
    //         SqlCommand mycommand = new SqlCommand("usp_testSp", cnn);
 //       mycommand.CommandType = CommandType.StoredProcedure;
  //      SqlDataAdapter adp = new SqlDataAdapter(mycommand);
  //      adp.Fill(ds);


        BusinessDealerList objBusiness = new BusinessDealerList();
        DataSet ds = objBusiness.GetDealerList();
        // ds = DealerCollection;
        EntityDealerList ObjEntity = new EntityDealerList();

        //DataSet ds1 = new DataSet();
        Document = CreateBaseStringBuilder();
        Document.AppendLine("<Dealer>");

//use for each to loop through each data
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            ObjEntity.Code = dr["Code"].ToString();
            ObjEntity.Name = dr["Name"].ToString();
            Document.AppendLine("<Dealer>");
            Document.AppendLine("<Code>" + ObjEntity.Code + "</Code>");
            Document.AppendLine("<Name>" + ObjEntity.Name + "</Name>");
            Document.AppendLine("</Dealer>");

        }
        Document.AppendLine("</Dealer>");
        XMLDocument.LoadXml(Document.ToString());


// Add this if you want to create an xml and write data in it
        // Save the document to a file 

        XmlTextWriter writer = new XmlTextWriter(@"C:\data.xml", null);
        writer.Formatting = Formatting.Indented;
        XMLDocument.Save(writer);

        return (XMLDocument);

    }


//inside webconfig add below 
 <connectionStrings>
    <remove name="LocalSqlServer" />

    <add name="
Connect" connectionString="data source=10.11.48.89;uid=sa;pwd=abcd;initial catalog=testDB" providerName="system.data.sqlclient"/>
   
  </connectionStrings>



No comments:

Post a Comment