コード例 #1
0
ファイル: XmlHelper.cs プロジェクト: armku/LearnPipe
        /// <summary>数据流转为Xml实体对象</summary>
        /// <param name="stream">数据流</param>
        /// <param name="type">实体类型</param>
        /// <param name="encoding">编码</param>
        /// <returns>Xml实体对象</returns>
        public static Object ToXmlEntity(this Stream stream, Type type, Encoding encoding = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            var x = new NewLife.Serialization.Xml
            {
                Stream   = stream,
                Encoding = encoding
            };

            return(x.Read(type));

            //if (!type.IsPublic) throw new XException("类型{0}不是public,不能进行Xml序列化!", type.FullName);

            //var serial = new XmlSerializer(type);
            //using (var reader = new StreamReader(stream, encoding))
            //using (var xr = new XmlTextReader(reader))
            //{
            //    // 必须关闭Normalization,否则字符串的\r\n会变为\n
            //    //xr.Normalization = true;
            //    return serial.Deserialize(xr);
            //}
        }
コード例 #2
0
ファイル: XmlHelper.cs プロジェクト: armku/LearnPipe
        /// <summary>字符串转为Xml实体对象</summary>
        /// <param name="xml">Xml字符串</param>
        /// <param name="type">实体类型</param>
        /// <returns>Xml实体对象</returns>
        public static Object ToXmlEntity(this String xml, Type type)
        {
            if (xml.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException("xml");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var x = new NewLife.Serialization.Xml
            {
                Stream = new MemoryStream(xml.GetBytes())
            };

            return(x.Read(type));

            //if (!type.IsPublic) throw new XException("类型{0}不是public,不能进行Xml序列化!", type.FullName);

            //var serial = new XmlSerializer(type);
            //using (var reader = new StringReader(xml))
            //using (var xr = new XmlTextReader(reader))
            //{
            //    // 必须关闭Normalization,否则字符串的\r\n会变为\n
            //    //xr.Normalization = true;
            //    return serial.Deserialize(xr);
            //}
        }