public static class XmlSerializeAdapter { public static T Deserialize<T>(XmlNode node) where T : class { return new XmlSerializer(typeof(T)).Deserialize(new XmlNodeReader(node)) as T; } public static IEnumerable<T> Deserialize<T>(XmlNodeList nodes) where T : class { List<T> results = new List<T>(); XmlSerializer serializer = new XmlSerializer(typeof(T)); foreach (XmlNode node in nodes) { results.Add(serializer.Deserialize(new XmlNodeReader(node)) as T); } return results; } public static object Deserialize(Type t, XmlNode node) { return new XmlSerializer(t).Deserialize(new XmlNodeReader(node)); } public static IEnumerable<object> Deserialize(Type t, XmlNodeList nodes) { List<object> results = new List<object>(); XmlSerializer serializer = new XmlSerializer(t); foreach (XmlNode node in nodes) { results.Add(serializer.Deserialize(new XmlNodeReader(node))); } return results; } public static object Deserialize(string type_name, XmlNode node) { return new XmlSerializer(Type.GetType(type_name)).Deserialize(new XmlNodeReader(node)); } public static IEnumerable<object> Deserialize(string type_name, XmlNodeList nodes) { List<object> results = new List<object>(); XmlSerializer serializer = new XmlSerializer(Type.GetType(type_name)); foreach (XmlNode node in nodes) { results.Add(serializer.Deserialize(new XmlNodeReader(node))); } return results; } public static string Serialize(object o) { try { String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(o.GetType()); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, o); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = Utils.UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } catch(Exception ex) { throw ex; } } public static T DeserializeInheritsObject<T, V>(XmlNode node) where T : class where V : class { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute to override the // field that returns Book objects. The overridden field // returns Expanded objects instead. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = typeof(T).Name; attr.Type = typeof(T); // Adds the XmlElementAttribute to the collection of objects. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(V), attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer xs = new XmlSerializer(typeof(T), attrOverrides); return xs.Deserialize(new XmlNodeReader(node)) as T; } public static string SerializeInheritsObject<T>(object o) where T : class { try { // Each overridden field, property, or type requires // an XmlAttributes instance. XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute instance to override the // field that returns Book objects. The overridden field // returns Expanded objects instead. XmlElementAttribute attr = new XmlElementAttribute(); attr.ElementName = o.GetType().Name; attr.Type = o.GetType(); // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); // Creates the XmlAttributeOverrides instance. XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); // Adds the type of the class that contains the overridden // member, as well as the XmlAttributes instance to override it // with, to the XmlAttributeOverrides. attrOverrides.Add(typeof(T), attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. String XmlizedString = null; XmlSerializer xs = new XmlSerializer(o.GetType(), attrOverrides); MemoryStream memoryStream = new MemoryStream(); ; XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, o); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = Utils.UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } catch(Exception ex) { throw ex; } } }
时间: 2024-11-03 20:49:12