Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

DictionarySerializer.cs

using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Nop.Services.Payments;

/// 
/// Dictionary serializer
/// 
public partial class DictionarySerializer : IXmlSerializable
{
    public DictionarySerializer()
    {
        Dictionary = new Dictionary();
    }

    /// 
    /// Ctor
    /// 
    /// Dictionary
    public DictionarySerializer(Dictionary dictionary)
    {
        Dictionary = dictionary;
    }

    /// 
    /// Write XML
    /// 
    /// Writer
    public void WriteXml(XmlWriter writer)
    {
        if (!Dictionary.Any())
            return;

        foreach (var key in Dictionary.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteElementString("key", key);
            var value = Dictionary[key];
            //please note that we use ToString() for objects here
            //of course, we can Serialize them
            //but let's keep it simple and leave it for developers to handle it
            //just put required serialization into ToString method of your object(s)
            //because some objects don't implement ISerializable
            //the question is how should we deserialize null values?
            writer.WriteElementString("value", value?.ToString());
            writer.WriteEndElement();
        }
    }

    /// 
    /// Read XML
    /// 
    /// Reader
    public void ReadXml(XmlReader reader)
    {
        var wasEmpty = reader.IsEmptyElement;
        reader.Read();
        if (wasEmpty)
            return;
        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            var key = reader.ReadElementString("key");
            var value = reader.ReadElementString("value");
            Dictionary.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }

        reader.ReadEndElement();
    }

    /// 
    /// Get schema
    /// 
    /// XML schema
    public XmlSchema GetSchema()
    {
        return null;
    }

    /// 
    /// Dictionary
    /// 
    public Dictionary Dictionary { get; }
}