示例#1
0
        /// <summary>
        /// Deserialize an XML string to an object of type T
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DeserializeXML <T>(this string value)
        {
            var serializer = new XmlSerializer(typeof(T));
            var reader     = new NamespaceIgnorantXmlTextReader(new StringReader(value)); //XmlReader.Create(value.Trim().ToStream());

            return((T)serializer.Deserialize(reader));
        }
示例#2
0
        /// <summary>
        /// Converts a SOAP string to an object
        /// </summary>
        /// <typeparam name="T">Object type</typeparam>
        /// <param name="soap">SOAP string</param>
        /// <returns>The object of the specified type</returns>
        public static T ToObject <T>(string soap)
        {
            if (string.IsNullOrEmpty(soap))
            {
                throw new ArgumentException("SOAP can not be null/empty");
            }

            StringReader stringReader = null;

            try
            {
                stringReader = new StringReader(soap);
                using (var reader = new NamespaceIgnorantXmlTextReader(stringReader))
                {
                    stringReader = null;
                    var formatter = new XmlSerializer(typeof(T));
                    return((T)formatter.Deserialize(reader));
                }
            }
            finally
            {
                if (stringReader != null)
                {
                    stringReader.Dispose();
                }
            }
        }
示例#3
0
    public static Display ParseRequest(string sXML)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Display));
        StringReader  sr         = new StringReader(sXML);
        NamespaceIgnorantXmlTextReader XMLWithoutNamespace = new NamespaceIgnorantXmlTextReader(sr);

        return((Display)serializer.Deserialize(XMLWithoutNamespace));
    }
示例#4
0
 public T Deserialize <T>(string xml)
 {
     try
     {
         var serializer = new XmlSerializer(typeof(T));
         using (var reader = new StringReader(xml))
         {
             using (var xmlTextReader = new NamespaceIgnorantXmlTextReader(reader))
             {
                 return((T)(serializer.Deserialize(xmlTextReader)));
             }
         }
     }
     catch (Exception exception)
     {
         return(default(T));
     }
 }
示例#5
0
        /// <summary>
        ///     Deserialize the provided <see cref="byte" /> array to a <typeparamref name="T1" /> instance
        /// </summary>
        /// <typeparam name="T1">A <typeparamref name="T" /> derived type specifying the target of deserialization</typeparam>
        /// <param name="stream">A <see cref="Stream" /> instance containing data to be deserialized</param>
        /// <returns>The <code>data</code> deserialized to <typeparamref name="T1" /> instance</returns>
        /// <exception cref="DeserializationException">The deserialization failed</exception>
        public T1 Deserialize <T1>(Stream stream) where T1 : T
        {
            using (var reader = new NamespaceIgnorantXmlTextReader(stream))
            {
                bool startElementFound;
                try
                {
                    startElementFound = reader.IsStartElement();
                }
                catch (XmlException ex)
                {
                    throw new DeserializationException("The format of the xml is not correct", stream.GetData(), null,
                                                       ex);
                }

                if (!startElementFound)
                {
                    throw new DeserializationException("Could not retrieve the name of the root element",
                                                       stream.GetData(), null, null);
                }

                var localName = reader.LocalName;
                SerializerWithInfo serializerWithInfo;
                if (!Serializers.TryGetValue(reader.LocalName, out serializerWithInfo))
                {
                    throw new DeserializationException("Specified root element is not supported", stream.GetData(),
                                                       localName, null);
                }
                reader.IgnoreNamespace = serializerWithInfo.IgnoreNamespace;

                try
                {
                    return((T1)serializerWithInfo.Serializer.Deserialize(reader));
                }
                catch (InvalidOperationException ex)
                {
                    throw new DeserializationException("Deserialization failed", stream.GetData(), localName,
                                                       ex.InnerException ?? ex);
                }
            }
        }