1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public static string XmlSerialize<T>(T obj) { XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add(string.Empty, string.Empty);
using (var memStm = new MemoryStream()) using (var xw = XmlWriter.Create(memStm)) { xsSubmit.Serialize(xw, obj, ns); byte[] utf8 = memStm.ToArray(); return Encoding.UTF8.GetString(utf8); } }
public static T XmlDeSerialize<T>(string xmlString) { XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
byte[] byteArray = Encoding.UTF8.GetBytes(xmlString);
using (var memStm = new MemoryStream(byteArray)) using (var xr = XmlReader.Create(memStm)) { object data = xsSubmit.Deserialize(xr); return (T)data; } }
|