/// <summary>
        /// Use Partner API to query SFDC records
        /// </summary>
        /// <param name="sessionId"></param>
        /// <param name="serverUrl"></param>
        private static void QueryPartnerRecords()
        {
            Console.WriteLine("Querying account records with the Partner API ...");

            //set query endpoint to value returned by login request
            EndpointAddress apiAddr = new EndpointAddress(serverUrl);

            partner.SessionHeader header = new partner.SessionHeader();
            header.sessionId = sessionId;

            //create service client to call API endpoint
            using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr))
            {
                //create SOQL query statement
                string query = "SELECT Name, AccountNumber, BillingState FROM Account";

                partner.QueryResult result = queryClient.query(
                    header, //sessionheader
                    null,   //calloptions
                    null,   //queryoptions
                    null,   //mruheader
                    null,   //packageversionheader
                    query   //query string
                    );

                //loop through results
                foreach (partner.sObject account in result.records)
                {
                    string acctName = account.Any[0].InnerText;
                    string acctNum  = account.Any.First(x => x.LocalName == "AccountNumber").InnerText;
                    Console.WriteLine(string.Format("Account Name: {0}, Account Number: {1}", acctName, acctNum));
                }

                Console.WriteLine("");
                Console.WriteLine("Query complete.");
                Console.ReadLine();
            }
        }
        /// <summary>
        /// Call SFDC endpoint and retrieve authentication token and API URL for SOAP callers
        /// </summary>
        private static void AuthenticateSfdcPartnerUser()
        {
            //print message to console
            Console.WriteLine("Authenticating against the Partner API ...");

            //use default binding and address from app.config
            using (partner.SoapClient loginClient = new partner.SoapClient("Soap1"))
            {
                //set account password and account token variables
                string sfdcPassword = "******";
                string sfdcToken    = "[token]";

                //set to Force.com user account that has API access enabled
                string sfdcUserName = "******";

                //create login password value
                string loginPassword = sfdcPassword + sfdcToken;

                //call Login operation from Enterprise WSDL
                partner.LoginResult result =
                    loginClient.login(
                        null, //LoginScopeHeader
                        null, //calloptions
                        sfdcUserName,
                        loginPassword);

                //get response values
                sessionId = result.sessionId;
                serverUrl = result.serverUrl;

                //print response values
                Console.WriteLine(string.Format("The session ID is {0} and server URL is {1}", sessionId, serverUrl));
                Console.WriteLine("");
                Console.WriteLine("Press [Enter] to continue ...");
                Console.ReadLine();
            }
        }
        /// <summary>
        /// Use Partner API to create new SFDC records
        /// </summary>
        private static void CreatePartnerRecords()
        {
            Console.WriteLine("Creating an account record with the Partner API ...");

            //set query endpoint to value returned by login request
            EndpointAddress apiAddr = new EndpointAddress(serverUrl);

            partner.SessionHeader header = new partner.SessionHeader();
            header.sessionId = sessionId;

            //create service client to call API endpoint
            using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr))
            {
                partner.sObject account = new partner.sObject();
                account.type = "Account";

                //create XML containers for necessary XML document and elements
                XmlDocument rootDoc = new XmlDocument();
                XmlElement[] accountFields = new XmlElement[3];

                XElement[] test = new XElement[2];
                test[0] = new XElement("node1", "value");

                //add fields
                accountFields[0] = rootDoc.CreateElement("Name");
                accountFields[0].InnerText = "DevForce06";
                accountFields[1] = rootDoc.CreateElement("AccountNumber");
                accountFields[1].InnerText = "1004441239";
                accountFields[2] = rootDoc.CreateElement("AnnualRevenue");
                accountFields[2].InnerText = "4000000";

                //set object property to array
                account.Any = accountFields;

                partner.SaveResult[] results;
            
                queryClient.create(
                    header, //sessionheader
                    null,   //calloptions
                    null,   //assignmentruleheader
                    null,   //mruheader
                    null,   //allowfieldtruncationheader
                    null,   //disablefeedtrackingheader
                    null,   //streamingenabledheader
                    null,   //allornothingheader
                    null,   //debuggingheader
                    null,   //packageversionheader
                    null,   //emailheader
                    new partner.sObject[] { account }, //new accounts
                    out results //result of create operation
                    );


                //only added one item, so looking at first index of results object
                if (results[0].success)
                {
                    Console.WriteLine("Account successfully created.");
                }

                Console.ReadLine();
            }
        }
        /// <summary>
        /// Use Partner API to query SFDC records
        /// </summary>
        /// <param name="sessionId"></param>
        /// <param name="serverUrl"></param>
        private static void QueryPartnerRecords()
        {
            Console.WriteLine("Querying account records with the Partner API ...");

            //set query endpoint to value returned by login request
            EndpointAddress apiAddr = new EndpointAddress(serverUrl);

            partner.SessionHeader header = new partner.SessionHeader();
            header.sessionId = sessionId;

            //create service client to call API endpoint
            using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr))
            {
                //create SOQL query statement
                string query = "SELECT Name, AccountNumber, BillingState FROM Account";

                partner.QueryResult result = queryClient.query(
                    header, //sessionheader
                    null, //calloptions
                    null, //queryoptions
                    null, //mruheader
                    null, //packageversionheader
                    query //query string
                    );

                //loop through results
                foreach (partner.sObject account in result.records)
                {
                    string acctName = account.Any[0].InnerText;
                    string acctNum = account.Any.First(x => x.LocalName == "AccountNumber").InnerText;
                    Console.WriteLine(string.Format("Account Name: {0}, Account Number: {1}", acctName, acctNum));

                }

                Console.WriteLine("");
                Console.WriteLine("Query complete.");
                Console.ReadLine();
            }
        }
        /// <summary>
        /// Call SFDC endpoint and retrieve authentication token and API URL for SOAP callers
        /// </summary>
        private static void AuthenticateSfdcPartnerUser(string _un,string _pw,string _token)
        {
            //print message to console
            Console.WriteLine("Authenticating against the Partner API ...");

            //use default binding and address from app.config
            using (partner.SoapClient loginClient = new partner.SoapClient("Soap1"))
            {
                //set account password and account token variables
                string sfdcPassword = _pw;
                string sfdcToken = _token;

                //set to Force.com user account that has API access enabled
                string sfdcUserName = _un;

                //create login password value
                string loginPassword = sfdcPassword + sfdcToken;

                //call Login operation from Enterprise WSDL
                partner.LoginResult result =
                    loginClient.login(
                    null, //LoginScopeHeader
                    null, //calloptions
                    sfdcUserName,
                    loginPassword);

                //get response values
                sessionId = result.sessionId;
                serverUrl = result.serverUrl;

                //print response values
                Console.WriteLine(string.Format("The session ID is {0} and server URL is {1}", sessionId, serverUrl));
                Console.WriteLine("");
                Console.WriteLine("Press [Enter] to continue ...");
                Console.ReadLine();
            }
        }
        /// <summary>
        /// Use Partner API to create new SFDC records
        /// </summary>
        private static void CreatePartnerRecords()
        {
            Console.WriteLine("Creating an account record with the Partner API ...");

            //set query endpoint to value returned by login request
            EndpointAddress apiAddr = new EndpointAddress(serverUrl);

            partner.SessionHeader header = new partner.SessionHeader();
            header.sessionId = sessionId;

            //create service client to call API endpoint
            using (partner.SoapClient queryClient = new partner.SoapClient("Soap1", apiAddr))
            {
                partner.sObject account = new partner.sObject();
                account.type = "Account";

                //create XML containers for necessary XML document and elements
                XmlDocument  rootDoc       = new XmlDocument();
                XmlElement[] accountFields = new XmlElement[3];

                XElement[] test = new XElement[2];
                test[0] = new XElement("node1", "value");

                //add fields
                accountFields[0]           = rootDoc.CreateElement("Name");
                accountFields[0].InnerText = "DevForce06";
                accountFields[1]           = rootDoc.CreateElement("AccountNumber");
                accountFields[1].InnerText = "1004441239";
                accountFields[2]           = rootDoc.CreateElement("AnnualRevenue");
                accountFields[2].InnerText = "4000000";

                //set object property to array
                account.Any = accountFields;

                partner.SaveResult[] results;

                queryClient.create(
                    header,                            //sessionheader
                    null,                              //calloptions
                    null,                              //assignmentruleheader
                    null,                              //mruheader
                    null,                              //allowfieldtruncationheader
                    null,                              //disablefeedtrackingheader
                    null,                              //streamingenabledheader
                    null,                              //allornothingheader
                    null,                              //debuggingheader
                    null,                              //packageversionheader
                    null,                              //emailheader
                    new partner.sObject[] { account }, //new accounts
                    out results                        //result of create operation
                    );


                //only added one item, so looking at first index of results object
                if (results[0].success)
                {
                    Console.WriteLine("Account successfully created.");
                }

                Console.ReadLine();
            }
        }