C
C#16mo ago
CatSandwich

✅ Configure Xml Serialization

Hey folks! I have a type like this:
class DateTimeWrapper
{
public DateTime Value;
}
class DateTimeWrapper
{
public DateTime Value;
}
I'm serializing this via System.Xml. I would like to have this type serialize as if I were serializing the wrapped Value, rather than the whole type. In other words, I want it to serialize as this:
2023-08-04 00:00:00
2023-08-04 00:00:00
rather than
<Value>
2023-08-04 00:00:00
</Value>
<Value>
2023-08-04 00:00:00
</Value>
What are my options to make this happen? I would ideally like to avoid changing the serializer itself, but rather use an approach that the serializer will respect. For example, newtonsoft json uses JsonConverter, but I'm not sure if System.Xml has a similar concept.
6 Replies
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
CatSandwich
CatSandwichOP16mo ago
Giving it a try now
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
CatSandwich
CatSandwichOP16mo ago
Yeah I'm hoping so. The only reason it wouldn't is because I simplified this down from the actual case, which is much more convoluted. Looks like it's halfway there. I didn't realize that it would also add <DateTimeWrapper>...</DateTimeWrapper> to the serialization Actually maybe that's fine, one sec So here's what I'm testing:
using System.Xml.Serialization;

var writer = new StringWriter();
var serializer = new XmlSerializer(typeof(Thing));
serializer.Serialize(writer, new Thing()
{
SystemTime = DateTime.Now,
DateTime = new DateTimeWrapper() { Value = DateTime.UtcNow },
DateTimes = new[]
{
new DateTimeWrapper() { Value = DateTime.UtcNow },
new DateTimeWrapper() { Value = DateTime.UtcNow },
new DateTimeWrapper() { Value = DateTime.UtcNow }
},
SystemTimes = new[]
{
DateTime.UtcNow,
DateTime.UtcNow,
DateTime.UtcNow,
}
});
Console.WriteLine(writer.ToString());

public class DateTimeWrapper
{
[XmlText]
public DateTime Value;
}

public class Thing
{
public DateTime SystemTime;
public DateTimeWrapper DateTime;
public DateTime[] SystemTimes;
public DateTimeWrapper[] DateTimes;
}
using System.Xml.Serialization;

var writer = new StringWriter();
var serializer = new XmlSerializer(typeof(Thing));
serializer.Serialize(writer, new Thing()
{
SystemTime = DateTime.Now,
DateTime = new DateTimeWrapper() { Value = DateTime.UtcNow },
DateTimes = new[]
{
new DateTimeWrapper() { Value = DateTime.UtcNow },
new DateTimeWrapper() { Value = DateTime.UtcNow },
new DateTimeWrapper() { Value = DateTime.UtcNow }
},
SystemTimes = new[]
{
DateTime.UtcNow,
DateTime.UtcNow,
DateTime.UtcNow,
}
});
Console.WriteLine(writer.ToString());

public class DateTimeWrapper
{
[XmlText]
public DateTime Value;
}

public class Thing
{
public DateTime SystemTime;
public DateTimeWrapper DateTime;
public DateTime[] SystemTimes;
public DateTimeWrapper[] DateTimes;
}
I want the wrappers to produce the exact same xml as the built ins Here's what I'm getting:
<?xml version="1.0" encoding="utf-16"?>
<Thing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SystemTime>2023-08-04T15:28:00.5183317-04:00</SystemTime>
<DateTime>2023-08-04T19:28:00.5218777Z</DateTime>
<SystemTimes>
<dateTime>2023-08-04T19:28:00.5218783Z</dateTime>
<dateTime>2023-08-04T19:28:00.5218784Z</dateTime>
<dateTime>2023-08-04T19:28:00.5218784Z</dateTime>
</SystemTimes>
<DateTimes>
<DateTimeWrapper>2023-08-04T19:28:00.5218779Z</DateTimeWrapper>
<DateTimeWrapper>2023-08-04T19:28:00.5218782Z</DateTimeWrapper>
<DateTimeWrapper>2023-08-04T19:28:00.5218782Z</DateTimeWrapper>
</DateTimes>
</Thing>
<?xml version="1.0" encoding="utf-16"?>
<Thing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SystemTime>2023-08-04T15:28:00.5183317-04:00</SystemTime>
<DateTime>2023-08-04T19:28:00.5218777Z</DateTime>
<SystemTimes>
<dateTime>2023-08-04T19:28:00.5218783Z</dateTime>
<dateTime>2023-08-04T19:28:00.5218784Z</dateTime>
<dateTime>2023-08-04T19:28:00.5218784Z</dateTime>
</SystemTimes>
<DateTimes>
<DateTimeWrapper>2023-08-04T19:28:00.5218779Z</DateTimeWrapper>
<DateTimeWrapper>2023-08-04T19:28:00.5218782Z</DateTimeWrapper>
<DateTimeWrapper>2023-08-04T19:28:00.5218782Z</DateTimeWrapper>
</DateTimes>
</Thing>
Everything is working except for the array element names I would ideally not like to control this on the consuming fields, but on the type itself XmlRoot didn't affect the element root names, unfortunately
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
CatSandwich
CatSandwichOP16mo ago
Wonderful, thanks! I'll give it a go monday. Fairly confident this is the last piece of the puzzle so I'll close.
Want results from more Discord servers?
Add your server