Serializing and Deserializing Dictionary<int, string> to Custom XML Without Using XElement in C#

Nov 30, 2025 · Programming · 14 views · 7.8

Keywords: C# | XML Serialization | Dictionary | Deserialization | XmlSerializer

Abstract: This technical paper provides an in-depth exploration of efficient techniques for converting Dictionary<int, string> to custom XML format and vice versa in C# development without relying on XElement. Through detailed analysis of temporary helper class design principles, XmlSerializer configuration methods, and LINQ applications in data transformation, it offers complete serialization and deserialization solutions. The paper also compares alternative XElement-based approaches and discusses considerations for serializing different dictionary types, providing practical guidance for handling complex data structure serialization scenarios.

Core Problem Analysis

In C# development, there is often a need to persist in-memory data structures to XML format or load data from XML files into memory objects. For key-value pair collections like Dictionary<int, string>, standard XML serialization mechanisms cannot handle them directly because the dictionary's key-value pair structure has no direct correspondence in XML representation.

The core issue lies in the XML serializer's expectation of objects with well-defined structures, while the dynamic nature of dictionaries makes direct serialization complex. Particularly when the XML format requires specific attribute structures, such as the example format <item id='int_goes_here' value='string_goes_here'/>, an intermediate conversion strategy is necessary.

Solution Design

Temporary Helper Class Design

To address dictionary serialization challenges, first define an intermediate transition class that accurately maps to the target XML structure:

public class Item
{
    [XmlAttribute]
    public int Id { get; set; }
    
    [XmlAttribute]
    public string Value { get; set; }
}

Key design considerations include:

Serializer Configuration

Special attention is required when configuring XmlSerializer for root element settings:

XmlSerializer serializer = new XmlSerializer(typeof(Item[]), 
    new XmlRootAttribute() { ElementName = "items" });

Configuration parameter explanation:

Implementation Details

Serialization Process

The process of converting a dictionary to XML involves both data transformation and serialization steps:

Dictionary<int, string> dictionary = new Dictionary<int, string>()
{
    {1, "one"}, 
    {2, "two"}
};

using (var stream = new MemoryStream())
{
    var items = dictionary.Select(kv => new Item() 
    { 
        Id = kv.Key, 
        Value = kv.Value 
    }).ToArray();
    
    serializer.Serialize(stream, items);
    
    string xmlContent = Encoding.UTF8.GetString(stream.ToArray());
}

Detailed serialization steps:

  1. Use LINQ's Select method to convert each key-value pair in the dictionary to an Item object
  2. Create an Item array using ToArray() method, which is the expected input type for the serializer
  3. Call the Serialize method to convert the object graph to XML format
  4. Process the output stream to obtain the final XML string

Deserialization Process

The process of restoring dictionary structure from XML also requires intermediate conversion:

using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent)))
{
    var deserializedItems = (Item[])serializer.Deserialize(stream);
    
    Dictionary<int, string> restoredDictionary = deserializedItems
        .ToDictionary(item => item.Id, item => item.Value);
}

Key deserialization points:

Alternative Approach Comparison

XElement-Based Implementation

Although the problem requires avoiding XElement, understanding alternative approaches provides comprehensive insight into the problem domain:

// Serialization
XElement xmlElement = new XElement("items",
    dictionary.Select(kv => new XElement("item",
        new XAttribute("id", kv.Key),
        new XAttribute("value", kv.Value))));

// Deserialization
XElement loadedElement = XElement.Parse(xmlElement.ToString());
Dictionary<int, string> parsedDictionary = loadedElement
    .Descendants("item")
    .ToDictionary(element => (int)element.Attribute("id"), 
                  element => (string)element.Attribute("value"));

Advantages and disadvantages of the XElement approach:

Extended Discussion

Generic Dictionary Serialization Solution

For more general dictionary serialization needs, consider implementing the IXmlSerializable interface:

public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    public XmlSchema GetSchema() => null;
    
    public void ReadXml(XmlReader reader)
    {
        // Implement custom XML reading logic
    }
    
    public void WriteXml(XmlWriter writer)
    {
        // Implement custom XML writing logic
    }
}

Considerations for this approach:

Performance Optimization Recommendations

In practical applications, serialization performance is an important consideration:

Practical Application Scenarios

This serialization pattern is particularly useful in the following scenarios:

Through the temporary helper class-based serialization approach introduced in this paper, developers can flexibly handle conversions between dictionary data and XML formats without relying on specific XML processing libraries, providing reliable data serialization solutions for various application scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.