我有問題反序列化XML文件到C#對象。該XML文件是這樣的:XML序列化程序不識別XmlElements
<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>
的Profile
對象:
[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; }
}
的Job
對象:
[Serializable]
[XmlType("Job")]
public class Job
{
[XmlElement("Job_Code")]
public string JobCode { get; set; }
[XmlElement("Status")]
public string Status { get; set; }
}
和代碼讀取和反序列化文件:
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);
}
當我運行此,我注意到串行不承認有三個Profile
對象,但是,它是不能夠反序列化各個屬性:ProfileID
和Job
爲空和LastUpdatedDate
具有默認DateTime值。我覺得我錯過了一些簡單的東西(可能在屬性中)。任何幫助表示讚賞。
這樣做。謝謝! – PoweredByOrange