title: XML Parsing Error - Salesforce Bulk API using C#
body: <p>I'm attempting to <a href="http://www.salesforce.com/us/developer/docs/api_asynch/index_CSH.htm#asynch_api_quickstart_create_job.htm" rel="nofollow noreferrer">Create a Job</a> and <a href="http://www.salesforce.com/us/developer/docs/api_asynch/index_CSH.htm#asynch_api_quickstart_add_batch.htm" rel="nofollow noreferrer">Add a Batch</a> using the Salesforce Bulk API, but I receive an <code>XML Parsing Error</code> back from Salesforce.</p>
  
  <p>As advised in the <a href="https://stackoverflow.com/questions/4326598/connecting-to-salesforce-bulk-api-using-c-sharp">Connecting to SalesForce bulk API using C#</a> question, I'm using the partner WSDL to perform the login; that and the Create Job call are working. (Before I had attempted to perform the login by POSTing XML as the <a href="http://www.salesforce.com/us/developer/docs/api_asynch/index_CSH.htm#asynch_api_quickstart_login.htm" rel="nofollow noreferrer">documentation</a> had mentioned; I couldn't get that to work, either.)</p>
  
  <p>Unfortunately, I'm a bit rusty with C#, but here is the code I'm working with. </p>
  
  <pre><code>...
  using SalesforceTest.sforce;
  ...
  
  namespace SalesforceTest
  {
      public partial class InvokeBulkAPI : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e) { }
  
          public string SalesforceLogin(string username, string password)
          {
              SforceService binding = new SforceService();
              binding.Timeout = 60000;
  
              try
              {
                  LoginResult lr = binding.login(username, password);
                  binding.Url = lr.serverUrl;
  
                  return lr.sessionId;
              }
              catch (SoapException e) { return e.Message; }
          }
  
          public string CreateJob(string sfSessionId, string sfOperation, string sfObjectName)
          {
              string str = "";
              string reqURL = "";
  
              byte[] bytes;
  
              XmlDocument reqDoc;
              XmlDocument respDoc;
  
              str = ""
                  + "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n" // added \r\n as recommended by L.B's answer
                  + "&lt;jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\"&gt;"
                  + "    &lt;operation&gt;&lt;/operation&gt;" // removed "+sfOperation+"
                  + "    &lt;object&gt;&lt;/object&gt;" // removed "+sfObjectName+"
                  + "    &lt;contentType&gt;XML&lt;/contentType&gt;" // should be CSV, NOT XML
                  + "&lt;/jobInfo&gt;"
              ;
  
              reqURL = "https://cs12-api.salesforce.com/services/async/23.0/job";
              reqDoc = new XmlDocument(); 
              reqDoc.LoadXml(str);
  
              // added XML modifications
              reqDoc.GetElementsByTagName("operation")[0].InnerText = sfOperation;
              reqDoc.GetElementsByTagName("object")[0].InnerText = sfObjectName;
  
              bytes = System.Text.Encoding.ASCII.GetBytes(reqDoc.InnerXml);
              respDoc = Post(bytes, reqURL, sfSessionId); // create job
  
              string JobId = (respDoc != null) ? 
                  (respDoc.GetElementsByTagName("id").Count &gt; 0) ?
                      (respDoc.GetElementsByTagName("id")[0].InnerText) :
                      "" :
                  ""
              ;
  
              return JobId;
          }
  
          public void AddBatch(string sfSessionId, string sfJobId, byte[] fileBytes)
          {
              string reqURL = "https://cs12-api.salesforce.com/services/async/23.0/job/" + sfJobId + "/batch";
              XmlDocument respDoc = Post(fileBytes, reqURL, sfSessionId);
          }
  
          public XmlDocument Post(byte[] bytes, string reqURL, string sfSessionId)
          {
              WebRequest req = WebRequest.Create(reqURL);
              req.Method = "POST";
              req.ContentLength = bytes.Length;
              req.ContentType = "application/xml; charset=UTF-8"; // should be text/csv; when passing a CSV file
              req.Headers.Add("X-SFDC-Session: " + sfSessionId);
  
              System.IO.Stream strm = req.GetRequestStream();
              strm.Write(bytes, 0, bytes.Length);
              strm.Close();
  
              WebResponse resp = req.GetResponse();
              System.IO.Stream respStrm = resp.GetResponseStream();
  
              XmlDocument respDoc = new XmlDocument();
              respDoc.Load(respStrm);
  
              return respDoc;
          }
  
          protected void Button1_Click(object sender, EventArgs e)
          {
              string SessionId = SalesforceLogin(this.TextBox1.Text, this.TextBox2.Text);
              string JobId = CreateJob(SessionId, "insert", "Contact");
  
              if (JobId.Length &gt; 0)
              {
                  AddBatch(SessionId, JobId, this.FileUpload1.FileBytes);
              }
          }
      }
  }
  </code></pre>
  
  <p>The file I'm posting is the same as the <a href="http://www.salesforce.com/us/developer/docs/api_asynch/index_CSH.htm#asynch_api_quickstart_add_batch.htm" rel="nofollow noreferrer">example</a> in the documentation.</p>
  
  <pre><code>FirstName,LastName,Department,Birthdate,Description
  Tom,Jones,Marketing,1940-06-07Z,"Self-described as ""the top"" branding guru on the West Coast"
  Ian,Dury,R&amp;D,,"World-renowned expert in fuzzy logic design. 
  Influential in technology purchases."
  </code></pre>
  
  <p>My question is <strong>why would I receive an XML Parsing Error from Salesforce on Line Number 1, Column 1</strong>?</p>
  
  <p>This is what I receive:</p>
  
  <pre><code>XML Parsing Error: syntax error
  Location: https://####.salesforce.com/services/async/23.0/job/###/batch/###/request
  Line Number 1, Column 1:
  
  FirstName,LastName,Department,Birthdate,Description
  ^
  </code></pre>
  
  <p>Any help would be appreciated. Thanks!</p>