无法反序列化XML

时间:2013-07-26 19:24:56

标签: c# xml xml-deserialization

我正在尝试反序列化以下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属性?我的代码将它作为属性(因此,它失败了)......

3 个答案:

答案 0 :(得分:3)

目前,您的代码期望xmlns为元素,而不是属性。

您应该将XmlSerialization attributes添加到您的课程中,如下所示:

[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文章中找到更多信息:

答案 1 :(得分: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);
}

答案 2 :(得分: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);