none
WCF Restful Consuming XML parse and Store the data in the Database RRS feed

  • Question

  • Hello,

    I am fairly new to WCF. What I am trying to do is create a WCF RESTUL that will consume an XML from the client and parse that and store it into the database. 

    I would appreciate if you can point me to the write direction.

    Thanks!

    Friday, March 28, 2014 3:10 PM

Answers

  • Hi,
    In wcf rest service, you can add a OperationContract to insert xml data to database, then please try to check the following about inserting xml data to database:

                string connetionString = null;
                SqlConnection connection;
                SqlCommand command ;
                SqlDataAdapter adpter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                XmlReader xmlFile ;
                string sql = null;
    
                int product_ID = 0;
                string Product_Name = null;
                double product_Price = 0;
    
                connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";
    
                connection = new SqlConnection(connetionString);
    
                xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
                ds.ReadXml(xmlFile);
                int i = 0;
                connection.Open();
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                    Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
                    sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
                    command = new SqlCommand(sql, connection);
                    adpter.InsertCommand = command;
                    adpter.InsertCommand.ExecuteNonQuery();
                }
                connection.Close();
       
            }
        }
    
    
     
     

    You have to pass necessary database connection information to connection string.

    And please click here to download the input file product.xml : product.xml .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, March 31, 2014 6:49 AM
    Moderator

All replies

  • Hi,
    In wcf rest service, you can add a OperationContract to insert xml data to database, then please try to check the following about inserting xml data to database:

                string connetionString = null;
                SqlConnection connection;
                SqlCommand command ;
                SqlDataAdapter adpter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                XmlReader xmlFile ;
                string sql = null;
    
                int product_ID = 0;
                string Product_Name = null;
                double product_Price = 0;
    
                connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";
    
                connection = new SqlConnection(connetionString);
    
                xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
                ds.ReadXml(xmlFile);
                int i = 0;
                connection.Open();
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                    Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
                    sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
                    command = new SqlCommand(sql, connection);
                    adpter.InsertCommand = command;
                    adpter.InsertCommand.ExecuteNonQuery();
                }
                connection.Close();
       
            }
        }
    
    
     
     

    You have to pass necessary database connection information to connection string.

    And please click here to download the input file product.xml : product.xml .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, March 31, 2014 6:49 AM
    Moderator
  • Thank you for your response. I tried to do this in my IService.cs

    But I am not getting what I want. just to be clear, I will feed an xml into my webservice display that in the browser and bind that XML into my database. 

    [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
             BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "MyHttpGetData/{Product.xml}")]
            string GetData(string value);
            
            string connetionString = null;
            SqlConnection connection;
            SqlCommand command ;
            SqlDataAdapter adpter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            XmlReader xmlFile ;
            string sql = null;

                int product_ID = 0;
                string Product_Name = null;
                double product_Price = 0;

                connetionString = "Data Source=servername;Initial Catalog=databsename;User ID=username;Password=password";

                connection = new SqlConnection(connetionString);

                xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
                ds.ReadXml(xmlFile);
                int i = 0;
                connection.Open();
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    product_ID = Convert.ToInt32(ds.Tables[0].Rows[i].ItemArray[0]);
                    Product_Name = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    product_Price = Convert.ToDouble(ds.Tables[0].Rows[i].ItemArray[2]);
                    sql = "insert into Product values(" + product_ID + ",'" + Product_Name + "'," + product_Price + ")";
                    command = new SqlCommand(sql, connection);
                    adpter.InsertCommand = command;
                    adpter.InsertCommand.ExecuteNonQuery();
                }
                connection.Close();
       
            }
        }

    Thursday, April 10, 2014 4:10 PM