XML Serializer无法识别XmlElements

时间:2014-02-20 18:40:32

标签: c# xml xml-deserialization

我遇到了将XML文件反序列化为C#对象的问题。 XML文件如下所示:

<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值。我觉得我缺少一些简单的东西(可能在属性中)。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

ProfileIDLastUpdated不是Xml 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; }
}

答案 1 :(得分:1)

这适用于您的示例x​​ml。我改变了所有与xml相关的属性:)

Jobs XmlElement,其他所有人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; }
}

答案 2 :(得分:0)

我注意到您的C#代码和您尝试映射的XML存在多个问题。

您是否序列化了上面发布的XML或使用编辑器输入的内容?

正如Selman22所说,XMLElements是XMLAttributes,对于Job元素,您提到了XMLArray(“Job”),但是在您的XML父节点中缺少Job元素,将Job元素的父节点更改为Jobs XMLArray(“Jobs”) )