cancel
Showing results for 
Search instead for 
Did you mean: 

Extract Campaign via XML - error

SOLVED
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!!

4 REPLIES 4
CTCT 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
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);
            }

 

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
Advisor

Re: Extract Campaign via XML - error

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