如何将嵌套类和集合序列化为XML

本文关键字:集合 序列化 XML 嵌套 | 更新日期: 2023-09-27 18:03:28

首先,我在互联网上尝试了不同的方法,但我没有得到我想要的。我有一个具有以下属性的视图模型类;

public class UserEntitySubmissionsReportViewModel
    {
        public UserEntitySubmissionsReportViewModel()
        {
            Submissions = new FinalUserEntitiesAssignmentViewModel();
        }
        public int Id { get; set; }
        public int Status { get; set; }
        public DateTime SubmissionDate { get; set; }
        public int UserEntityAssignmentId { get; set; }
        public FinalUserEntitiesAssignmentViewModel Submissions { get; set; }
    }

和嵌套类FinalUserEntitiesAssignmentViewModel,它是;

[Serializable]
public class FinalUserEntitiesAssignmentViewModel
{
    public FinalUserEntitiesAssignmentViewModel()
    {
        ProjectInformation = new ProjectInformationViewModel();
        MilestoneInformation = new MilestoneInformationViewModel();
        ActivityListInformation = new ActivityListInformationViewModel();
        ActivityInformation = new ActivityInformationViewModel();
        SubActivityInformation = new List<SubActivityInformationViewModel>();
    }
    [XmlElement(ElementName = "ProjectInformation")]
    public ProjectInformationViewModel ProjectInformation { get; set; }
    [XmlElement(ElementName = "MilestoneInformation")]
    public MilestoneInformationViewModel MilestoneInformation { get; set; }
    [XmlElement(ElementName = "ActivityListInformation")]
    public ActivityListInformationViewModel ActivityListInformation { get; set; }
    [XmlElement(ElementName = "ActivityInformation")]
    public ActivityInformationViewModel ActivityInformation { get; set; }
    [XmlElement(ElementName = "SubActivityInformation")]
    public List<SubActivityInformationViewModel> SubActivityInformation { get; set; }
}
[Serializable]
public class ProjectInformationViewModel
{
    [XmlElement(ElementName = "Id")]
    public int Id { get; set; }
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }
    public string Description { get; set; }
}

当我序列化这个时,我只得到1属性,即嵌套类的Id。

    var obj = new UserEntitySubmissionsReportViewModel();
    var writer = new System.Xml.Serialization.XmlSerializer(typeof(UserEntitySubmissionsReportViewModel));
    System.IO.StreamWriter file = new System.IO.StreamWriter(Server.MapPath("~/App_Data/UserEntitySubmissionsReportViewModel.xml"));
    writer.Serialize(file, obj);
    file.Close();

结果是;

<?xml version="1.0" encoding="utf-8"?>
<UserEntitySubmissionsReportViewModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Id>0</Id>
  <Status>0</Status>
  <SubmissionDate>0001-01-01T00:00:00</SubmissionDate>
  <UserEntityAssignmentId>0</UserEntityAssignmentId>
  <Submissions>
    <ProjectInformation>
      <Id>0</Id>
    </ProjectInformation>
    <MilestoneInformation>
      <Id>0</Id>
    </MilestoneInformation>
    <ActivityListInformation>
      <Id>0</Id>
    </ActivityListInformation>
    <ActivityInformation>
      <Id>0</Id>
      <Attributes />
      <Tools />
    </ActivityInformation>
  </Submissions>
</UserEntitySubmissionsReportViewModel>

如您所见,我不能序列化其他属性。同样,我也有巢收集。如何使用c#序列化嵌套属性

如何将嵌套类和集合序列化为XML

我认为,在序列化XML时,您需要为您的类定义默认构造函数。尝试为您的ProjectInformationViewModel类添加一个构造。

public class ProjectInformationViewModel
{
    // Default Constructor
    public ProjectInformationViewModel()
    {
    }
}