Exchange Web Services Managed API Error: Expected XML node type was XmlDeclaration, but actual type is Element


I have a simple test app to verify some functionality in Exchange Web Services Managed API. When Executing the code below I receive the error:

"Expected XML node type was XmlDeclaration, but actual type is Element"

I think I may have a permissions problem or some other issue but I have no idea where to look and an hoping someone has seen this before.
It looks like the error is being returned during the CertificateValidationCallback.


 The source code in C# is attached.

Any help would be appreciated. I have been beating my head against a wall on this one.
using System;
using System.Windows.Forms;
using Microsoft.Exchange.WebServices.Data;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace RestoreTest
{

    public partial class Form1 : Form
    {

        public Form1()
        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }

        public static bool CertificateValidationCallBack(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
   System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {

            // If the certificate is a valid, signed certificate, return true.
            if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
            {

                return true;

            }

            // If thre are errors in the certificate chain, look at each error to determine the cause.
            if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
            {

                if (chain != null && chain.ChainStatus != null)
                {

                    foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                    {
                        if ((certificate.Subject == certificate.Issuer) &&
                           (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                        {
                            // Self-signed certificates with an untrusted root are valid. 
                            continue;
                        }
                        else
                        {
                            if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                            {
                                // If there are any other errors in the certificate chain, the certificate is invalid,
                                // so the method returns false.
                                return false;
                            }
                        }
                    }

                }

                // When processing reaches this line, the only errors in the certificate chain are 
                // untrusted root errors for self-signed certifcates. These certificates are valid
                // for default Exchange server installations, so return true.
                return true;

            }
            else
            {

                // In all other cases, return false.
                return false;

            }

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            // Validate the server certificate.
            // For a certificate validation code example, see the Validating X509 Certificates topic in the Core Tasks section.            
            try
            {

                ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
                // Connect to Exchange Web Services as user1 at contoso.com.
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.Credentials = new WebCredentials("bob", "P@ssword123","icodecentral.local");
                service.Url = new Uri(@"https://localhost/EWS/Exchange.asmx");
                //service.AutodiscoverUrl("bob@icodecentral.local");

                // Create the e-mail message, set its properties, and send it to user2@contoso.com, saving a copy to the Sent Items folder. 
                EmailMessage message = new EmailMessage(service);
                message.Subject = "Interesting";
                message.Body = "The proposition has been considered.";
                message.ToRecipients.Add("claire@icodecentral.local");
                message.SendAndSaveCopy();

                // Write confirmation message to console window.
                MessageBox.Show("Message sent!");                

            }
            catch (Exception ex)
            {

                MessageBox.Show("Error: " + ex.Message);

            }

        }

    }
}

Open in new window

mark_chAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark_chAuthor Commented:
The issue was caused by the Exchange Web Services being configured incorrectly. The EWS Virtual directory was using ASP.Net 1.1. Upon changing the ASP.Net version to 2.0 the problem was resolved.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.