Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

asp.net 2.0 xml

Below web service codes are working fine. But I forgot I can only use .net framework 2.0.
And if I take out using System.xml.linq the code will break because it uses Xelement and Xdocument, and etc.

Unfortuneately, I have to stay in framework 2.0 and anyone knows how to modify below codes working with .net framework 2.0?

Thanks

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;

namespace WebApplication12
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {      
        [WebMethod]
        public string GetCoachList(Boolean isUpload, string uploadPath, int? locationID, string passCode)
        {

            string errorMessage = "";
            string myDatas = "";
            string defaultXmlFileName = "test.xml";

            //Connection string is stored
            //in the web.config file as an appSetting
            string connectionString = "Data Source=LENOVO-PC;Initial Catalog=DatabaseName;Integrated Security=True";
            SqlConnection dbConnection = null;
            // Open a connection to the database
            try
            {
                dbConnection = new SqlConnection(connectionString);
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            if (errorMessage == "")
            {
                   string SQL = "SELECT * from test";
                SqlCommand GetCustomerCmd = new SqlCommand(SQL, dbConnection);
                try
                {
                    XDocument results = new XDocument(new XElement("results"));
                    using (XmlReader reader = GetCustomerCmd.ExecuteXmlReader())
                    {
                        reader.Read();
                        while (!reader.EOF)
                        {
                            myDatas = myDatas + reader.ReadOuterXml();
                        }                        
                    }
                    dbConnection.Close();
                }
                catch (System.Exception ex)
                {
                    errorMessage = ex.Message;
                }
                finally
                {
                    dbConnection.Dispose();
                }
            }

            #region do you want load and save the document?
            if (isUpload == true)
            {
                if (!string.IsNullOrEmpty(uploadPath))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml("<root>" + myDatas + "</root>");
                    doc.PreserveWhitespace = true;
                    doc.Save(uploadPath + defaultXmlFileName);
                }
            }
            #endregion

            if (passCode != "abc")
            {
                myDatas = null;
            }
            return myDatas;
        }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial