XML分析错误-使用C#的Salesforce Bulk API

本文关键字:Salesforce Bulk API 使用 错误 XML | 更新日期: 2023-09-27 18:22:00

我正在尝试使用Salesforce Bulk API创建作业和添加批次,但我收到了Salesforce返回的XML Parsing Error

根据使用C#问题连接到SalesForce批量API中的建议,我使用合作伙伴WSDL来执行登录;和创建作业呼叫正在工作。(在我尝试通过文档中提到的张贴XML来执行登录之前;我也无法实现这一点。)

不幸的是,我对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);
            }
        }
    }
}

我发布的文件与文档中的示例相同。

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."

我的问题是为什么我会在第1行第1列收到Salesforce的XML解析错误

这就是我收到的:

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
^

如有任何帮助,我们将不胜感激。谢谢

XML分析错误-使用C#的Salesforce Bulk API

首先,不要使用字符串操作创建xml。使用XmlDocument或XDocument之类的XML解析器。

Xml声明应在单独的行<?xml version='"1.0'" encoding='"UTF-8'"?>上。你忘了'r'n

第三,当发送csv 时,您应该将text/csv设置为内容类型

该示例使用curl发送csv。您正在向需要xml的服务发送csv。找到一个带有xml数据的示例,或者尝试使用与curl相同的params进行调用。