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)); }
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()); }