我正在尝试反序列化以下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;
}
}
...
}
要正确反序列化,我缺少什么?
这表明我应该将其读为字符串:
How to use HttpClient to read an XML response?
但这表明它应该“正常工作”
HttpClient ReadAsAsync<Type> only deserializing part of the response
我也尝试过(在使用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; }
...
}
答案 0 :(得分:0)
嗯,我不是肥皂专家,但如果我没记错的话,应该在属性上使用[DataMember]这样的修饰符,并在类上使用[DataContract(NameSpace =“ jobInfo”))这样的修饰符。 您可以看到,在第一个建议中,您提供给该人使用[XmlRoot]和[XmlElement]
您还可以看到WCF Datacontract, some fields do not deserialize,也许对您有帮助
答案 1 :(得分:0)
使用
测试后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