3

我正在尝试反序列化以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<jobInfo
xmlns="http://www.force.com/2009/06/asyncapi/dataload">
<id>750x0000000005LAAQ</id>
<operation>insert</operation>
<object>Contact</object>
<createdById>005x0000000wPWdAAM</createdById>
<createdDate>2009-09-01T16:42:46.000Z</createdDate>
<systemModstamp>2009-09-01T16:42:46.000Z</systemModstamp>
<state>Open</state>
<concurrencyMode>Parallel</concurrencyMode>
<contentType>CSV</contentType>
<numberBatchesQueued>0</numberBatchesQueued>
<numberBatchesInProgress>0</numberBatchesInProgress>
<numberBatchesCompleted>0</numberBatchesCompleted>
<numberBatchesFailed>0</numberBatchesFailed>
<numberBatchesTotal>0</numberBatchesTotal>
<numberRecordsProcessed>0</numberRecordsProcessed>
<numberRetries>0</numberRetries>
<apiVersion>28.0</apiVersion>
</jobInfo>

我创建了这个对象:

public class JobInfo
{
    public string xmlns { get; set; }
    public string id { get; set; }
    public string operation { get; set; }
    public string @object { get; set; }
    public string createdById { get; set; }
    public string createdDate { get; set; }
    public string systemModstamp { get; set; }
    public string state { get; set; }
    public string concurrencyMode { get; set; }
    public string contentType { get; set; }
    public string numberBatchesQueued { get; set; }
    public string numberBatchesInProgress { get; set; }
    public string numberBatchesCompleted { get; set; }
    public string numberBatchesFailed { get; set; }
    public string numberBatchesTotal { get; set; }
    public string numberRecordsProcessed { get; set; }
    public string numberRetries { get; set; }
    public string apiVersion { get; set; }
}

public class RootObject
{
    public JobInfo jobInfo { get; set; }
}

而且,我正在使用这段代码:

XmlSerializer serializer = new XmlSerializer(typeof(RootObject));
StringReader rdr = new StringReader(response);
RootObject resultingMessage = (RootObject)serializer.Deserialize(rdr);

但是,我收到此错误:

There is an error in XML document (1, 40).

{"<jobInfo xmlns='http://www.force.com/2009/06/asyncapi/dataload'> was not expected."}

我该如何解释该xmlns属性?我的代码将它作为一个属性(因此,它失败了)......

4

3 回答 3

3

当前,您的代码需要xmlnsas 元素,而不是属性。

您应该将XmlSerialization属性添加到您的类中,如下所示:

[XmlRoot("jobInfo", Namespace="http://www.force.com/2009/06/asyncapi/dataload"]
public class JobInfo
{
    [XmlAttribute]
    public string xmlns { get; set; }
    [XmlElement(ElementName = "id")]
    public string id { get; set; }
    ....
}

您可以在 MSDN 文章中找到更多信息:

于 2013-07-26T19:31:48.043 回答
2

尝试使用XmlRootAttribute来指定Namespace.

[XmlRoot("jobInfo", Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
public class JobInfo
{
    [XmlElement("id")]
    public string Id { get; set; }

    [XmlElement("operation")]
    public string Operation { get; set; }

    [XmlElement("object")]
    public string Object { get; set; }

    [XmlElement("createdById")]
    public string CreatedById { get; set; }

    [XmlElement("createdDate")]
    public DateTime CreatedDate { get; set; }

    [XmlElement("systemModstamp")]
    public DateTime SystemModstamp { get; set; }

    [XmlElement("state")]
    public string State { get; set; }

    [XmlElement("concurrencyMode")]
    public string ConcurrencyMode { get; set; }

    [XmlElement("contentType")]
    public string ContentType { get; set; }

    [XmlElement("numberBatchesQueued")]
    public string NumberBatchesQueued { get; set; }

    [XmlElement("numberBatchesInProgress")]
    public string NumberBatchesInProgress { get; set; }

    [XmlElement("numberBatchesCompleted")]
    public string NumberBatchesCompleted { get; set; }

    [XmlElement("numberBatchesFailed")]
    public string NumberBatchesFailed { get; set; }

    [XmlElement("numberBatchesTotal")]
    public string NumberBatchesTotal { get; set; }

    [XmlElement("numberRecordsProcessed")]
    public string numberRecordsProcessed { get; set; }

    [XmlElement("numberRetries")]
    public string NumberRetries { get; set; }

    [XmlElement("apiVersion")]
    public string ApiVersion { get; set; }
}

代码:

var xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<jobInfo xmlns=""http://www.force.com/2009/06/asyncapi/dataload"">
    <id>750x0000000005LAAQ</id>
    <operation>insert</operation>
    <object>Contact</object>
    <createdById>005x0000000wPWdAAM</createdById>
    <createdDate>2009-09-01T16:42:46.000Z</createdDate>
    <systemModstamp>2009-09-01T16:42:46.000Z</systemModstamp>
    <state>Open</state>
    <concurrencyMode>Parallel</concurrencyMode>
    <contentType>CSV</contentType>
    <numberBatchesQueued>0</numberBatchesQueued>
    <numberBatchesInProgress>0</numberBatchesInProgress>
    <numberBatchesCompleted>0</numberBatchesCompleted>
    <numberBatchesFailed>0</numberBatchesFailed>
    <numberBatchesTotal>0</numberBatchesTotal>
    <numberRecordsProcessed>0</numberRecordsProcessed>
    <numberRetries>0</numberRetries>
    <apiVersion>28.0</apiVersion>
</jobInfo>";

var serializer = new XmlSerializer(typeof(JobInfo));

JobInfo jobInfo;

using(var stream = new StringReader(xml))
using(var reader = XmlReader.Create(stream))
{
    jobInfo = (JobInfo)serializer.Deserialize(reader);
}
于 2013-07-26T19:36:47.087 回答
1

添加命名空间

  [XmlRoot("jobInfo",Namespace = "http://www.force.com/2009/06/asyncapi/dataload")]
  public class JobInfo
  {
     // get rid of the xmlns property
     // other properties stay
  }

// there is no rootobject, JobInfo is your root
XmlSerializer serializer = new XmlSerializer(typeof(JobInfo));
StringReader rdr = new StringReader(response);
JobInfo resultingMessage = (JobInfo)serializer.Deserialize(rdr);
于 2013-07-26T19:31:44.460 回答