Beispiel #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));
        }
Beispiel #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, CultureInfo.InvariantCulture);

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

            return(sb.ToString());
        }