public static object XmlStr2Obj(Type type, string xml) { if (type == null) { throw new ArgumentNullException("type"); } if (xml == null) { throw new ArgumentException("Xml is null or empty"); } if (xml == string.Empty) { return(Activator.CreateInstance(type)); } StringReader reader = new StringReader(xml); XmlSerializer sr = SerializerCache.GetSerializer(type); return(sr.Deserialize(reader)); }
/// <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()); }