1

First, i tried on the internet different ways, but i didn't got what i want. i have viewmodel class with the following properties;

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

and the nested class FinalUserEntitiesAssignmentViewModel whcih is;

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

When i serialize this, i only get the 1 property i.e Id for nested class.

    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();

The result i get is;

<?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>

As you can see, i am not able to serialize other properties. Similarly i have nest collection too. How can i serialize nested properties using C# ?

Idrees Khan
  • 7,702
  • 18
  • 63
  • 111
  • 1
    Have you tried setting values for the properties that don't get output by the serializer? `Id` has a default value of 0 but all the other properties have a default value of `null`... –  May 14 '14 at 08:33
  • @DaveParsons no, but i only need a schema from these classes. its a complex nested view model – Idrees Khan May 14 '14 at 08:36
  • http://stackoverflow.com/questions/711723/xml-serialization-and-null-value-c-sharp or http://stackoverflow.com/questions/3935582/change-how-xmlserializer-serializes-empty-elements –  May 14 '14 at 08:36
  • I'm not sure why you're serialising ViewModel classes. Wouldn't the Name and Id properties be better represented as XML Attributes? An easy way to get what you want is to write the expected output in XML then use Visual Studio's very helpful Paste XML as classes facility. – Evil Dog Pie May 14 '14 at 08:39
  • @MikeofSST, i need this xml schema for crystal report – Idrees Khan May 14 '14 at 08:40
  • I think @DaveParsons has provided a couple of good answers. :-) – Evil Dog Pie May 14 '14 at 08:42

1 Answers1

0

I think that when serializing XML that you need to define a default constructor for your classes. Try adding a construction for your ProjectInformationViewModel class.

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

    }
}
iCode
  • 1,254
  • 1
  • 13
  • 16