3

I am trying to deserialize the following 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>

I have created this object:

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; }
}

And, I am using this code:

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

However, I get this error:

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

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

How can I account for the xmlns attribute? My code has it as a property (thus, it fails)...

user1477388
  • 20,790
  • 32
  • 144
  • 264

3 Answers3

3

Currently your code is expecting the xmlns as element, not the attribute.

You should add the XmlSerialization attributes to your class, like this:

[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; }
    ....
}

You can find more information in MSDN articles:

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • FYI, in my test, this works except the `xmlns` property is still not populated. Not sure if this a limitation or a misconfiguration of attributes. – Chris Sinclair Jul 26 '13 at 19:35
  • Move the Namespace name to the constant, and populate it from there - this will not change until the XML schema change. – VMAtm Jul 26 '13 at 19:36
2

Try using the XmlRootAttribute to specify the 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; }
}

Code:

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);
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
1

Add the namespace

  [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);
rene
  • 41,474
  • 78
  • 114
  • 152