xml处理类 xmlAdapter
Posted If_1989
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml处理类 xmlAdapter相关的知识,希望对你有一定的参考价值。
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; } } }
以上是关于xml处理类 xmlAdapter的主要内容,如果未能解决你的问题,请参考以下文章
java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件
C#.NET 程序员的福利,自己写的一个XML操作类,可实现像jquery一样方便的xml操作,且不用专门去处理命名空间。