2012-01-30 35 views
1

我試圖使用Salesforce批量API Create a JobAdd a Batch,但我從Salesforce收到XML Parsing ErrorXML分析錯誤 - 使用C#的Salesforce批量API

按照Connecting to SalesForce bulk API using C#問題的建議,我使用夥伴WSDL來執行登錄;這和創建工作呼叫正在工作。 (在我試圖通過POSTing XML進行登錄之前,documentation已經提到過;我無法得到它的工作)

不幸的是,我對C#有些生疏,但這裏是代碼我正在合作。

... 
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 = "" 
       + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" // added \r\n as recommended by L.B's answer 
       + "<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">" 
       + " <operation></operation>" // removed "+sfOperation+" 
       + " <object></object>" // removed "+sfObjectName+" 
       + " <contentType>XML</contentType>" // should be CSV, NOT XML 
       + "</jobInfo>" 
      ; 

      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 > 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 > 0) 
      { 
       AddBatch(SessionId, JobId, this.FileUpload1.FileBytes); 
      } 
     } 
    } 
} 

我發佈的文件與文檔中的example相同。

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&D,,"World-renowned expert in fuzzy logic design. 
Influential in technology purchases." 

我的問題是爲什麼我會收到一個XML從Salesforce對1號線分析錯誤,列1

這是我收到:

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 
^ 

任何幫助,將不勝感激。謝謝!

回答

4

首先,不要使用字符串操作創建xml。使用XML解析器,如XmlDocument或XDocument。

Xml聲明應該在單獨的行<?xml version=\"1.0\" encoding=\"UTF-8\"?>。您發送CSV時

+0

優秀的建議,謝謝忘記\r\n

第三,你應該設置text/csv內容類型! 我在XML聲明中添加了'\ r \ n',刪除了字符串操作(改用'reqDoc.GetElementsByTagName(「operation」)[0] .InnerText = sfOperation;'),將CreateBatch XML改爲使用'CSV '作爲contentType,並將AddBatch Post方法調用改爲傳遞'text/csv'。隨着這些修改,一切正常,批次已成功添加。 自從我與這些東西合作已經很久了。 – 2012-01-31 15:56:49

2

該示例使用curl發送csv。您正在將csv發送給期待xml的服務。用xml數據查找示例或嘗試使用與捲曲相同的參數進行調用。