2014-02-20 68 views
0

我有問題反序列化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對象,但是,它是不能夠反序列化各個屬性:ProfileIDJob爲空和LastUpdatedDate具有默認DateTime值。我覺得我錯過了一些簡單的東西(可能在屬性中)。任何幫助表示讚賞。

回答

1

這適用於您的示例x​​ml。我改變了所有的XML相關的屬性:)

JobsXmlElement,對於所有其他XmlAttribute

(順便說一句:你不需要XmlTypeSerializable屬性)

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

這樣做。謝謝! – PoweredByOrange

1

ProfileIDLastUpdatedXml Elements。它們attributes。使用XmlAttribute代替

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

而且你需要更改JobCodeStatus

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

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

固定它爲'ProfileID'和'LastUpdated',但Jobs'列表是st生病了。有什麼建議麼? – PoweredByOrange

+0

問題在於'XmlArray'屬性。它應該是'XmlElement'。 – PoweredByOrange

0

我注意到在你的C#代碼的多個問題和您正在嘗試映射的XML。

你是否已經序列化了上面發佈的或使用編輯器鍵入的XML?

正如Selman22所說,XMLElements是XMLAttributes,對於Job元素您提到了一個XMLArray(「Job」),但是在Job元素的XML父節點中缺少Job元素的父節點,請將其更改爲Jobs XMLArray(「Jobs 「)