Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there is as <code>Dictionary&lt;&gt;</code> itself isn't XML serializable, when I had need to send a dictionary object via a web service I ended up wrapping the <code>Dictionary&lt;&gt;</code> object myself and adding support for <code>IXMLSerializable</code>.</p> <pre><code>/// &lt;summary&gt; /// Represents an XML serializable collection of keys and values. /// &lt;/summary&gt; /// &lt;typeparam name="TKey"&gt;The type of the keys in the dictionary.&lt;/typeparam&gt; /// &lt;typeparam name="TValue"&gt;The type of the values in the dictionary.&lt;/typeparam&gt; [XmlRoot("dictionary")] public class SerializableDictionary&lt;TKey, TValue&gt; : Dictionary&lt;TKey, TValue&gt;, IXmlSerializable { #region Constants /// &lt;summary&gt; /// The default XML tag name for an item. /// &lt;/summary&gt; private const string DEFAULT_ITEM_TAG = "Item"; /// &lt;summary&gt; /// The default XML tag name for a key. /// &lt;/summary&gt; private const string DEFAULT_KEY_TAG = "Key"; /// &lt;summary&gt; /// The default XML tag name for a value. /// &lt;/summary&gt; private const string DEFAULT_VALUE_TAG = "Value"; #endregion #region Protected Properties /// &lt;summary&gt; /// Gets the XML tag name for an item. /// &lt;/summary&gt; protected virtual string ItemTagName { get { return DEFAULT_ITEM_TAG; } } /// &lt;summary&gt; /// Gets the XML tag name for a key. /// &lt;/summary&gt; protected virtual string KeyTagName { get { return DEFAULT_KEY_TAG; } } /// &lt;summary&gt; /// Gets the XML tag name for a value. /// &lt;/summary&gt; protected virtual string ValueTagName { get { return DEFAULT_VALUE_TAG; } } #endregion #region Public Methods /// &lt;summary&gt; /// Gets the XML schema for the XML serialization. /// &lt;/summary&gt; /// &lt;returns&gt;An XML schema for the serialized object.&lt;/returns&gt; public XmlSchema GetSchema() { return null; } /// &lt;summary&gt; /// Deserializes the object from XML. /// &lt;/summary&gt; /// &lt;param name="reader"&gt;The XML representation of the object.&lt;/param&gt; public void ReadXml(XmlReader reader) { XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); bool wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) { return; } while (reader.NodeType != XmlNodeType.EndElement) { reader.ReadStartElement(ItemTagName); reader.ReadStartElement(KeyTagName); TKey key = (TKey)keySerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement(ValueTagName); TValue value = (TValue)valueSerializer.Deserialize(reader); reader.ReadEndElement(); this.Add(key, value); reader.ReadEndElement(); reader.MoveToContent(); } reader.ReadEndElement(); } /// &lt;summary&gt; /// Serializes this instance to XML. /// &lt;/summary&gt; /// &lt;param name="writer"&gt;The writer to serialize to.&lt;/param&gt; public void WriteXml(XmlWriter writer) { XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); foreach (TKey key in this.Keys) { writer.WriteStartElement(ItemTagName); writer.WriteStartElement(KeyTagName); keySerializer.Serialize(writer, key); writer.WriteEndElement(); writer.WriteStartElement(ValueTagName); TValue value = this[key]; valueSerializer.Serialize(writer, value); writer.WriteEndElement(); writer.WriteEndElement(); } } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload