예제 #1
0
        public static T XmlStr2Obj <T>(string xml)
        {
            if (xml == null)
            {
                return(default(T));
            }
            if (xml == string.Empty)
            {
                return((T)Activator.CreateInstance(typeof(T)));
            }

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

            return((T)sr.Deserialize(reader));
        }
예제 #2
0
        public static string Obj2XmlStr(object obj)
        {
            if (obj == null)
            {
                return(string.Empty);
            }
            XmlSerializer sr = SerializerCache.GetSerializer(obj.GetType());

            StringBuilder sb = new StringBuilder();
            StringWriter  w  = new StringWriter(sb, System.Globalization.CultureInfo.InvariantCulture);

            sr.Serialize(
                w,
                obj,
                new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty) }));

            return(sb.ToString());
        }