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

mark_ch
mark_ch used Ask the Experts™
on

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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial