Showing results for 
Search instead for 
Did you mean: 

Extract Campaign via XML - error


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

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

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("" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("" +
                uName + "/campaigns");
            request.Credentials = LoginCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream str = response.GetResponseStream();
            byte[] inBuf = new byte[100000];

                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);

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



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

Re: Extract Campaign via XML - error

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