//----------------------------------------------------------------------- // // Copyright (c) 2009 iron9light // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // // iron9light //----------------------------------------------------------------------- namespace iron9light.Utility.Xml { using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Serialization; /// /// The session or dictionary that can serialize and deserialize to a standard xml document. /// public class XmlSession { private const string XmlTemplate = "<{0} xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">{1}"; private readonly Dictionary dict = new Dictionary(); /// /// Gets or sets the entries. /// /// The entries. [XmlArray("entries")] [XmlArrayItem("entry")] public Entry[] Entries { get { Entry[] entries = new Entry[this.dict.Count]; int i = 0; foreach (KeyValuePair pair in this.dict) { entries[i++] = new Entry(pair.Key, pair.Value); } return entries; } set { this.dict.Clear(); foreach (Entry entry in value) { this.dict[entry.Key] = GetValue(entry); } } } /// /// Gets or sets the with the specified key. /// /// The key. /// The value of this key or null if no value with this key in the session. public object this[string key] { get { object o; if (!this.dict.TryGetValue(key, out o)) { o = null; } return o; } set { if (value != null) { this.dict[key] = value; } else { this.dict.Remove(key); } } } /// /// Deserializes the specified stream. /// /// The stream. /// The session. public static XmlSession Deserialize(Stream stream) { XmlSerializer serializer = new XmlSerializer(typeof(XmlSession)); return (XmlSession)serializer.Deserialize(stream); } /// /// Deserializes the specified text reader. /// /// The text reader. /// The session. public static XmlSession Deserialize(TextReader textReader) { XmlSerializer serializer = new XmlSerializer(typeof(XmlSession)); return (XmlSession)serializer.Deserialize(textReader); } /// /// Deserializes the specified XML reader. /// /// The XML reader. /// The session. public static XmlSession Deserialize(XmlReader xmlReader) { XmlSerializer serializer = new XmlSerializer(typeof(XmlSession)); return (XmlSession)serializer.Deserialize(xmlReader); } /// /// Serializes this session the specified stream. /// /// The stream. public void Serialize(Stream stream) { XmlSerializer serializer = this.GetSerializer(); serializer.Serialize(stream, this); } /// /// Serializes this session the specified text writer. /// /// The text writer. public void Serialize(TextWriter textWriter) { XmlSerializer serializer = this.GetSerializer(); serializer.Serialize(textWriter, this); } /// /// Serializes this session the specified XML writer. /// /// The XML writer. public void Serialize(XmlWriter xmlWriter) { XmlSerializer serializer = this.GetSerializer(); serializer.Serialize(xmlWriter, this); } private static object GetValue(Entry entry) { object o = entry.Value; Type type = entry.Type; if (o.GetType() != type) { XmlNode[] xmlNodes = (XmlNode[])o; XmlElement xmlElement = (XmlElement)xmlNodes[1].ParentNode; string s = string.Format(XmlTemplate, type.Name, xmlElement.InnerXml); XmlSerializer serializer = new XmlSerializer(type); o = serializer.Deserialize(new StringReader(s)); } return o; } private XmlSerializer GetSerializer() { List types = new List(); foreach (object o in this.dict.Values) { Type type = o.GetType(); if (!types.Contains(type)) { types.Add(type); } } return new XmlSerializer(typeof(XmlSession), types.ToArray()); } } }