using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var toSerialize = new TimelineDocument
.Builder()
.Build();

var serializer = new XmlSerializer( typeof( TimelineDocument ) );
var stream = new MemoryStream();
var writer = new StreamWriter( stream );
serializer.Serialize( writer, toSerialize );

stream.Seek( 0, SeekOrigin.Begin );
var xml = new StreamReader( stream ).ReadToEnd();
Debug.Print( xml );

stream.Seek(0, SeekOrigin.Begin);
var deserialized = serializer.Deserialize( new StreamReader( stream ) ) as TimelineDocument;
// Inspect the deserialized variable at this point using the debugger
// or add code to dump the document content
// or add code to deep-compare toSerialize with deserializer
// (see https://stackoverflow.com/questions/1539989/c-sharp-implementation-of-deep-recursive-object-comparison-in-net-3-5#1540266
// for a possible solution).

}
}

public abstract class TimelineItemBase
{
public Guid InternalId { get; set; }
public string Title { get; set; }
public DateTime StartDateTime { get; set; }
}

public class TimelineMilestone : TimelineItemBase
{

}

public class TimelineTask : TimelineItemBase
{
public long DurationTicks { get; set; } // 10 million ticks in a second
public int? PercentageCompleted { get; set; }
}

public class TimelineDocument
{
public Guid InternalId { get; set; }
[XmlArrayItem(typeof( TimelineTask), ElementName = "Task")]
[XmlArrayItem(typeof(TimelineMilestone), ElementName = "Milestone")]
public List<TimelineItemBase> Items { get; set; }

internal class Builder
{
public TimelineDocument Build()
{
var doc = new TimelineDocument();
doc.InternalId = Guid.NewGuid();
doc.Items = new List<TimelineItemBase>()
{
new TimelineTask()
{
InternalId = Guid.NewGuid(),
Title ="Task 1",
StartDateTime = new DateTime( 2016, 12,1 ),
DurationTicks = 1000000,
PercentageCompleted = null
},
new TimelineMilestone()
{
InternalId = Guid.NewGuid(),
Title = "M 1",
StartDateTime = new DateTime( 2016, 12, 31 )
}
};

return doc;
}
}
}
}

 

Advertisements