using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml.Serialization; namespace SiwiFms.Helper { public class XmlHelper { /// /// xml字符串转对象 /// /// /// /// public static T DeserializeXmlStr(string xmlStr) { if (string.IsNullOrWhiteSpace(xmlStr)) return default(T); try { using (StringReader sr = new StringReader(xmlStr)) { XmlSerializer xs = new XmlSerializer(typeof(T)); return (T)xs.Deserialize(sr); } } catch (Exception ex) { Console.WriteLine(ex.Message); return default(T); } } /// /// xml序列化 /// /// /// public static string SerializeXmlStr(object obj) { if (obj == null) return string.Empty; MemoryStream stream = new MemoryStream(); StreamReader sr = null; try { XmlSerializer xs = new XmlSerializer(obj.GetType()); xs.Serialize(stream, obj); stream.Position = 0; sr = new StreamReader(stream); return sr.ReadToEnd(); } catch (Exception ex) { Console.WriteLine(ex.Message); return ""; } finally { if (sr != null) { sr.Dispose(); } stream.Dispose(); } } } }