コード例 #1
0
        public static T XmlStr2Obj <T>(string xml)
        {
            if (xml == null)
            {
                throw new ArgumentException("Xml is null or empty");
            }
            if (xml == string.Empty)
            {
                return((T)Activator.CreateInstance(typeof(T)));
            }

            StringReader  reader = new StringReader(xml);
            XmlSerializer sr     = SerializerCache.GetSerializer(typeof(T));        //new XmlSerializer(type);

            return((T)sr.Deserialize(reader));
        }
コード例 #2
0
        /// <summary>
        /// Сериализует объект в строку XML
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        public static string Obj2XmlStr(object obj, string nameSpace)
        {
            if (obj == null)
            {
                return(string.Empty);
            }
            XmlSerializer sr = SerializerCache.GetSerializer(obj.GetType());             //new XmlSerializer(obj.GetType(), nameSpace);

            StringBuilder sb = new StringBuilder();
            //MemoryStream stream = new MemoryStream();
            //XmlTextWriter writer = new XmlTextWriter(stream, Settings.Encoding);
            //writer.Formatting = Formatting.Indented;
            StringWriterUTF8 w = new StringWriterUTF8(sb, System.Globalization.CultureInfo.InvariantCulture);

            sr.Serialize(
                w,
                obj);
            //sb.Append(Settings.Encoding.GetChars(stream.ToArray()));
            //sb.Remove(0,40);
            return(sb.ToString());
        }