Say I have the following class:
public class Test
{
public string FullName { get; set; }
public int Score { get; set; }
public DateTime StartDate { get; set; }
}
I then create an instance:
Test t = new Test
{
FullName = "Bob Smith",
Score = 3.4,
StartDate = DateTime.Now
};
I use the XmlSerializer to convert to class to XML
using (var textWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter))
{
var serializer = new XmlSerializer(typeof(Test), CreateOverrides());
serializer.Serialize(xmlWriter, t);
}
xml = textWriter.ToString();
}
So I end up with this:
<Test>
<FullName>Bob Smith</FullName>
<Score>3.4</Score>
<StartDate>2018-03-12T13:24:19.9284562+00:00</StartDate>
</Test>
What I'd like to do if use some sort of override so I can format the XML. So let's say I'd like to see the following output:
<Test>
<FullName>First Name: Bob. Surname: Smith</FullName>
<Score>3.4000</Score>
<StartDate>2018 (this year) 03 (Spring) 12 (middle period)</StartDate>
</Test>
If there any way I can format or change the XML output based on a type e.g. Date, Int, etc by passing something into the serializer. I've been looking at XmlAttributeOverrides but I can't seem to change the content.
private static XmlAttributeOverrides CreateOverrides()
{
//Do something here to change the content...
//if int then change the format to 4 decimal places
//if date then change the date into a custom format
//etc
}
I need to do this via the serializer. I don't want to modify the existing Test class with attributes to ignore and replace xml elements.
Is what I'm trying to do even possible?