Exemplo n.º 1
0
        /// <summary>
        /// deserialize string.to an object.
        /// </summary>
        /// <param name="xml">the string need to be deserialized.</param>
        /// <param name="type">the type of the object.</param>
        /// <returns>the deserialized object.</returns>
        public static object XmlToObject(string xml, Type type)
        {
            if (xml == null)
            {
                //throw new ArgumentNullException("xml");
                Debug.WriteLine(String.Format("无xml信息:{0}\r\n{1}", xml, type.FullName));
                return(null);
            }
            if (type == null)
            {
                //throw new ArgumentNullException("type");
                Debug.WriteLine(String.Format("无type信息:{0}\r\n{1}", xml, type.FullName));
            }
            object        obj1       = null;
            XmlSerializer serializer = new XmlSerializer(type);

            using (StringReader reader = new StringReader(xml))
            {
                XmlReader reader2 = new NamespaceIgnorantXmlTextReader(reader);
                //XmlReader reader2 = new XmlTextReader(reader);
                try
                {
                    obj1 = serializer.Deserialize(reader2);
                }
                catch (InvalidOperationException exception1)
                {
                    Debug.WriteLine(CONVERT_EXCEPTION_MSG + exception1.Message);
                }
                finally
                {
                    reader2.Close();
                }
            }
            return(obj1);
        }
 /// <summary>
 /// deserialize string.to an object.
 /// </summary>
 /// <param name="xml">the string need to be deserialized.</param>
 /// <param name="type">the type of the object.</param>
 /// <returns>the deserialized object.</returns>
 public static object XmlToObject(string xml, Type type)
 {
     if (xml == null)
     {
         //throw new ArgumentNullException("xml");
         Debug.WriteLine(String.Format("无xml信息:{0}\r\n{1}", xml, type.FullName));
         return null;
     }
     if (type == null)
     {
         //throw new ArgumentNullException("type");
         Debug.WriteLine(String.Format("无type信息:{0}\r\n{1}", xml, type.FullName));
     }
     object obj1 = null;
     XmlSerializer serializer = new XmlSerializer(type);
     using (StringReader reader = new StringReader(xml))
     {
         XmlReader reader2 = new NamespaceIgnorantXmlTextReader(reader);
         //XmlReader reader2 = new XmlTextReader(reader);
         try
         {
             obj1 = serializer.Deserialize(reader2);
         }
         catch (InvalidOperationException exception1)
         {
             Debug.WriteLine(CONVERT_EXCEPTION_MSG + exception1.Message);
         }
         finally
         {
             reader2.Close();
         }
     }
     return obj1;
 }