Question

I'm having issues deserializing an XML file to a C# object. The XML file looks like this:

<ArrayOfProfile>
  <Profile ProfileID="14010001" LastUpdated="2014-02-18T11:33:05.430">
    <Job Job_Code="A     " Status="N " />
    <Job Job_Code="A     " Status="N " />
  </Profile>
  <Profile ProfileID="14010002" LastUpdated="2014-02-18T11:36:02.560">
      <Job Job_Code="A      " Status="N " />
  </Profile>
  <Profile ProfileID="14010003" LastUpdated="2014-02-17T11:23:21.850">
      <Job Job_Code="B      " Status="N " />
      <Job Job_Code="B      " Status="EN" />
      <Job Job_Code="C      " Status="N " />
  </Profile>
</ArrayOfProfile>

The Profile object:

[XmlRoot("ArrayOfProfile")]
[XmlType("Profile")]
public class Profile
{
    [XmlElement("ProfileID")]
    public string ProfileID { get; set; }

    [XmlElement("LastUpdated")]
    public DateTime LastUpdatedDate { get; set; }

    [XmlArray("Job")]
    public List<Job> Jobs { get; set; }
}

The Job object:

[Serializable]
[XmlType("Job")]
public class Job
{
    [XmlElement("Job_Code")]
    public string JobCode { get; set; }

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

And the code to read and deserialize the file:

XmlSerializer serializer = new XmlSerializer(typeof(List<Profile>), new Type[] { typeof(Job) });

using (StreamReader reader = new StreamReader(xmlFileToRead))
{
    List<Profile> profiles = (List<Profile>)serializer.Deserialize(reader);
}

When I run this, I notice the serializer does recognize that there are three Profile objects, however, it is not able to deserialize individual properties: ProfileID and Job are null and LastUpdatedDate has the default DateTime value. I feel like I'm missing something simple (probably in the attributes). Any help is appreciated.

Was it helpful?

Solution

This works for your sample xml. I changed all xml related attributes :)

for Jobs XmlElement, for all others XmlAttribute

(BTW: You don't need XmlType and Serializable attributes)

XmlSerializer serializer = new XmlSerializer(typeof(List<Profile>));

using (StreamReader reader = new StreamReader(File.Open(filename,FileMode.Open)))
{
    var profiles = (List<Profile>)serializer.Deserialize(reader);
}

public class Profile
{
    [XmlAttribute("ProfileID")]
    public string ProfileID { get; set; }

    [XmlAttribute("LastUpdated")]
    public DateTime LastUpdatedDate { get; set; }

    [XmlElement("Job")]
    public List<Job> Jobs { get; set; }
}

public class Job
{
    [XmlAttribute("Job_Code")]
    public string JobCode { get; set; }

    [XmlAttribute("Status")]
    public string Status { get; set; }
}

OTHER TIPS

ProfileID and LastUpdated are not Xml Elements.They are attributes.Use XmlAttribute instead

[XmlRoot("ArrayOfProfile")]
[XmlType("Profile")]
public class Profile
{
    [XmlAttribute("ProfileID")]
    public string ProfileID { get; set; }

    [XmlAttribute("LastUpdated")]
    public DateTime LastUpdatedDate { get; set; }

    [XmlArray("Job")]
    public List<Job> Jobs { get; set; }
}

Also you need to change JobCode and Status too

[Serializable]
[XmlType("Job")]
public class Job
{
    [XmlAttribute("Job_Code")]
    public string JobCode { get; set; }

    [XmlAttribute("Status")]
    public string Status { get; set; }
}

I am noticing multiple problems in your C# code and the XML you are trying to map.

Have you serialized the XML what is posted above or you typed using an editor?

As Selman22 said, XMLElements are XMLAttributes, for Job elements you mentioned an XMLArray("Job"), but in your XML parent node for Job elements are missing, change the parent node of Job elements to Jobs XMLArray("Jobs")

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top