Thread-safe, cached XML serialization helpers for .NET.
- Thread-Safe: Safely use
XmlSerializerin parallel scenarios. - Cached: Caches serializer instances to avoid memory leaks and performance hits.
- Simple API: Static
Xml.SerializeandXml.Deserializemethods.
var xml = Xml.Serialize(myObject);
var obj = Xml.Deserialize<MyType>(xml);await Parallel.ForEachAsync(records, new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
}, async (record, ct) =>
{
// Safe to call concurrently
var xml = Xml.Serialize(record);
await ProcessAsync(xml, ct);
});PLINQ Usage
var results = records
.AsParallel()
.WithDegreeOfParallelism(Environment.ProcessorCount)
.Select(record => Xml.Deserialize<MyType>(record.Xml))
.ToList();Standard XmlSerializer has memory leak issues if not used carefully (e.g., when using non-standard constructors). This library manages the caching and thread safety for you.
Thread 1 ──┬── GetSerializer<Order>() ──┐
Thread 2 ──┤ ├── Same cached Lazy<XmlSerializer>
Thread 3 ──┤ │ (created once, shared safely)
Thread 4 ──┘ │
▼
┌─────────────────┐
│ XmlSerializer │ (single instance)
│ (thread-safe │
│ after creation)│
└─────────────────┘