The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Extract Campaign via XML - error

SOLVED
Go to solution
Highlighted
Advisor

Extract Campaign via XML - error

Hi there, 

 

I am using an API through C# to get all of my campaign details via XML. It was working before, but now I get an error inside the ouputted XML file that comes from constant and contact. Apparently, there is an invalid line in this XML file.

 

This is the error I get.

 

XML Parsing Error: not well-formedLocation: file://Users/Desktop/Campaigns.xmlLine Number 269, Column 14:</feed>1.633Z</Date>-------------^

 

Please help!!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: Extract Campaign via XML - error

The problem is in your FileStream() constructor.  You are using FileMode.OpenOrCreate to open the file.  What this does is leave all the previous content in the file.  What can happen is that if you pull a campaign with less XML contet and write it to the file, it will leave the left over XML from the previous longer campaign in there.  Chances are, this will lead to XML that is badly formated since it will likely just be close tags or incomplete tags.  Correct solutions is to use FileMode.Create, which will create if no file exists or overwrite an existing file (which is the desired behavior in your code).

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

4 REPLIES 4
Highlighted
Employee

Re: Extract Campaign via XML - error

Hey Strategic,

 

Is it only a specific campaign that causes this issue or is it any campaign that is requested?  Also if you can post your code or your file so i can take a look at it, I will then be able to further troubleshoot your issue. 

 

 

Ryan Davis
Quality Engineer
Highlighted
Advisor

Re: Extract Campaign via XML - error

Its when getting all the campaigns. 

string uName = "abc";
            string pWord = "xyz";
            string APIKey = "apikey";
            ////Response.Redirect("Flyer2.aspx?dealerid=" + txtLogin.Text);
            CredentialCache LoginCredentials = new CredentialCache();
            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +
                uName + "/campaigns");
            request.Credentials = LoginCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream str = response.GetResponseStream();
            byte[] inBuf = new byte[100000];

            try
            {
                int bytesToRead = Convert.ToInt32(inBuf.Length);
                int bytesRead = 0;
                while (bytesToRead > 0)
                {
                    int n = str.Read(inBuf, bytesRead, bytesToRead);
                    if (n == 0)
                    {
                        break; // TODO: might not be correct. Was : Exit While
                    }
                    bytesRead += n;
                    bytesToRead -= n;
                }
                FileStream fstr = new FileStream(@"\\User\Desktop\Campaigns.xml", FileMode.OpenOrCreate, FileAccess.Write);
                fstr.Write(inBuf, 0, bytesRead);
                str.Close();
                fstr.Close();
            }

            catch (Exception ex)
            {
                throw new Exception("Exception: " + ex.Message);
            }

 

Highlighted
Moderator

Re: Extract Campaign via XML - error

The problem is in your FileStream() constructor.  You are using FileMode.OpenOrCreate to open the file.  What this does is leave all the previous content in the file.  What can happen is that if you pull a campaign with less XML contet and write it to the file, it will leave the left over XML from the previous longer campaign in there.  Chances are, this will lead to XML that is badly formated since it will likely just be close tags or incomplete tags.  Correct solutions is to use FileMode.Create, which will create if no file exists or overwrite an existing file (which is the desired behavior in your code).

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

Highlighted
Advisor

Re: Extract Campaign via XML - error

Thanks Dave. Also, thanks for the explanation. That solved it.

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured