博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xmlhelper (把实体对象转成xml文档)
阅读量:7004 次
发布时间:2019-06-27

本文共 4750 字,大约阅读时间需要 15 分钟。

08:55:14 2012-03-19 public static class XmlHelper    {        #region 实体类序列化成xml        ///         ///  实体类序列化成xml        ///         /// The enitities.        /// The headtag.        /// 
public static string ObjListToXml
(List
enitities, string headtag) { StringBuilder sb = new StringBuilder(); PropertyInfo[] propinfos = null; sb.AppendLine("
"); sb.AppendLine("<" + headtag + ">"); foreach (T obj in enitities) { //初始化propertyinfo if (propinfos == null) { Type objtype = obj.GetType(); propinfos = objtype.GetProperties(); } sb.AppendLine("
"); foreach (PropertyInfo propinfo in propinfos) { sb.Append("<"); sb.Append(propinfo.Name); sb.Append(">"); sb.Append(propinfo.GetValue(obj, null)); sb.Append("
"); } sb.AppendLine("
"); } sb.AppendLine("
"); return sb.ToString(); } #endregion #region 使用XML初始化实体类容器 ///
/// 使用XML初始化实体类容器 /// ///
///
The typename. ///
The XML. ///
The headtag. ///
public static List
XmlToObjListByNode
(string xml, string headtag) where T : new() { List
list = new List
(); XmlDocument doc = new XmlDocument(); PropertyInfo[] propinfos = null; doc.LoadXml(xml); XmlNodeList nodelist = doc.GetElementsByTagName(headtag); foreach (XmlNode node in nodelist) { T entity = new T(); //初始化propertyinfo if (propinfos == null) { Type objtype = entity.GetType(); propinfos = objtype.GetProperties(); } //填充entity类的属性 foreach (PropertyInfo propinfo in propinfos) { XmlNode cnode = node.SelectSingleNode(propinfo.Name); if (cnode == null) continue; string v = cnode.InnerText; if (v != null) propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null); } list.Add(entity); } return list; } #endregion #region 使用XML初始化实体类容器 ///
/// 使用XML初始化实体类容器 /// ///
///
The typename. ///
The XML. ///
The headtag. ///
public static List
XmlToObjListByAttr
(string xml, string headtag) where T : new() { List
list = new List
(); XmlDocument doc = new XmlDocument(); PropertyInfo[] propinfos = null; doc.LoadXml(xml); XmlNodeList nodelist = doc.GetElementsByTagName(headtag); foreach (XmlNode node in nodelist) { T entity = new T(); //初始化propertyinfo if (propinfos == null) { Type objtype = entity.GetType(); propinfos = objtype.GetProperties(); } //填充entity类的属性 foreach (PropertyInfo propinfo in propinfos) { XmlNode cnode = node.Attributes[propinfo.Name.ToLower()]; if (cnode == null) continue; string v = cnode.InnerText; if (v != null) propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null); } list.Add(entity); } return list; } #endregion ///
/// 将对象转换为二进制流 /// ///
///
public static byte[] ConvertToXmlStream(this object item) { var serializer = new XmlSerializer(item.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.Serialize(ms, item); var bytes = ms.ToArray(); ms.Close(); return bytes; } } ///
/// 在Xml源中查找指定节点文本 /// ///
///
///
public static List
GetNodeInnerText(string originXml, string nodeTag) { List
strList = new List
(); XmlDocument doc = new XmlDocument(); doc.LoadXml(originXml); XmlNodeList nodes = doc.GetElementsByTagName(nodeTag); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; strList.Add(node.InnerText); } return strList; } ///
/// 使用XML初始化实体类容器 /// ///
///
The typename. ///
The XML. ///
The headtag. ///
public static List
XmlToObjList
(string xml, string headtag) where T : new() { List
list = new List
(); XmlDocument doc = new XmlDocument(); PropertyInfo[] propinfos = null; doc.LoadXml(xml); XmlNodeList nodelist = doc.GetElementsByTagName(headtag); foreach (XmlNode nodef in nodelist) { XmlNodeList itemNodelist = nodef.ChildNodes; foreach (XmlNode node in itemNodelist) { T entity = new T(); //初始化propertyinfo if (propinfos == null) { Type objtype = entity.GetType(); propinfos = objtype.GetProperties(); } //填充entity类的属性 foreach (PropertyInfo propinfo in propinfos) { XmlNode cnode = node.SelectSingleNode(propinfo.Name); if (cnode == null) continue; string v = cnode.InnerText; if (v != null) propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null); } list.Add(entity); } } return list; } }

  

转载地址:http://kcytl.baihongyu.com/

你可能感兴趣的文章