Beispiel #1
0
        /// <summary>
        /// Serializes an Object to an XML-string.
        /// </summary>
        /// <typeparam name="T">The Type of the Object</typeparam>
        /// <param name="data">The Object</param>
        public static string WriteXmlDataInMemory <T>(T data)
        {
            StringBuilder output = new StringBuilder();

            using (StringWriter textWriter = new StringWriter(output))
            {
                XmlSerializer serializer = XmlSerializationTools.GetXmlSerializer(typeof(T));
                serializer.Serialize(textWriter, data);
            }

            return(output.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves XML-Serialized data from a string.
        /// </summary>
        /// <param name="xml">The serialized object</param>
        /// <param name="type">The Type of the data to deserialize</param>
        /// <returns>The deserialized object</returns>
        public static object ReadXmlDataInMemory(string xml, Type type)
        {
            if (xml == null)
            {
                throw new ArgumentNullException(nameof(xml));
            }

            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            using (MemoryStream memStream = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
            {
                XmlSerializer serializer = XmlSerializationTools.GetXmlSerializer(type);
                return(serializer.Deserialize(memStream));
            }
        }