[英]ReadAsAsync Class using XmlMediaTypeFormatter or other Deserialization
我正在嘗試反序列化以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<id>asjdkfljasl;kdf</id>
<operation>query</operation>
<object>jsdkfjsakldjakl</object>
...
</jobInfo>
我有以下代碼可以發出POST請求並成功運行,但是無法反序列化到我的課程中。
client.DefaultRequestHeaders.Add("X-SFDC-Session", binding.SessionHeaderValue.sessionId);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", binding.SessionHeaderValue.sessionId);
var content = new StringContent(createJobXml, Encoding.UTF8, "application/xml");
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
HttpResponseMessage response = await client.PostAsync(
$"https://{SERVER_INSTANCE}.salesforce.com/services/async/43.0/job", content
);
response.EnsureSuccessStatusCode();
jobInfo job = await response.Content.ReadAsAsync<jobInfo >(new List<MediaTypeFormatter>() {
new XmlMediaTypeFormatter { UseXmlSerializer = true },
new JsonMediaTypeFormatter()
});
但是我得到了錯誤
沒有MediaTypeFormatter可用於從媒體類型為'application / xml'的內容讀取'jobInfo'類型的對象,
我的jobInfo是使用xsd.exe doc.xml
, xsd.exe doc.xsd /classes
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.force.com/2009/06/asyncapi/dataload", IsNullable = false)]
public partial class jobInfo
{
private string idField;
private string operationField;
private string objectField;
...
public string id
{
get {
return this.idField;
}
set {
this.idField = value;
}
}
public string operation
{
get {
return this.operationField;
}
set {
this.operationField = value;
}
}
...
}
為了正確反序列化,我缺少什么?
這表明我應該將其作為字符串讀取:
但這表明它應該“正常工作”
HttpClient ReadAsAsync <Type>僅反序列化響應的一部分
我也嘗試過(在使用xsd將xml轉換為類之前使用過Bulkv1Job類)
[DataContract]
public class Bulkv1Job
{
[DataMember]
string id { get; set; }
[DataMember]
string operation { get; set; }
[DataMember]
string @object { get; set; }
...
}
和
[XmlRoot("jobInfo")]
public class Bulkv1Job
{
[XmlElement("id")]
string id { get; set; }
[XmlElement("operation")]
string operation { get; set; }
...
}
嗯,我不是肥皂專家,但如果我記得很好的話,應該在屬性上使用[DataMember]這樣的修飾符,在類上使用[DataContract(NameSpace =“ jobInfo”))這樣的修飾符。 您可以看到,在第一個建議中,您提供給該人使用[XmlRoot]和[XmlElement]
您還可以看到WCF Datacontract,某些字段未反序列化 ,也許會對您有所幫助
經過測試
var s = await response.content.readasstringasync();
var buffer = encoding.utf8.getbytes(s);
using (var stream = new memorystream(buffer)) {
var serializer = new xmlserializer(typeof(jobinfo)); // FAILED LINE
var jobinfo = (jobinfo)serializer.deserialize(stream);
//then do whatever you want
return jobinfo;
}
我發現序列化程序由於類保護錯誤而在上一行失敗。 確保jobInfo
類是公共的且可訪問的,錯誤消失並且deserialize(stream)
工作。
之后,刪除該代碼並使用readAsAsync<jobInfo>(...)
工作。
謝謝@Ryan
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.