XmlUtils.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Reflection;
  7. using System.Collections.Specialized;
  8. using System.Xml.Serialization;
  9. using System.IO;
  10. namespace Ant.Frame
  11. {
  12. //XML操作工具类代码
  13. /// <summary>
  14. /// xml操作工具类
  15. /// </summary>
  16. public class XmlUtils
  17. {
  18. #region Fields
  19. /// <summary>
  20. /// xml文件物理地址
  21. /// </summary>
  22. private string xmlFilePath = String.Empty;
  23. #endregion
  24. #region Constructor
  25. /// <summary>
  26. /// 构造
  27. /// </summary>
  28. public XmlUtils()
  29. {
  30. }
  31. /// <summary>
  32. /// 构造
  33. /// </summary>
  34. /// <param name="path">xml文件物理地址</param>
  35. public XmlUtils(string path)
  36. {
  37. xmlFilePath = path;
  38. }
  39. #endregion
  40. #region Methods
  41. /// <summary>
  42. /// 读取xml数据,并转换成对象
  43. /// </summary>
  44. /// <typeparam name="T">对象类名</typeparam>
  45. /// <param name="obj">类实例对象</param>
  46. /// <param name="nodeName">节点名称</param>
  47. public void LoadXml<T>(ref T obj, string nodeName)
  48. {
  49. Type oType = obj.GetType();
  50. XmlDocument oXmlDocument = new XmlDocument();
  51. oXmlDocument.Load(xmlFilePath);
  52. foreach (XmlNode oXmlNode in oXmlDocument.SelectSingleNode(nodeName).ChildNodes)
  53. {
  54. string name = oXmlNode.Name;
  55. string value = oXmlNode.InnerText;
  56. foreach (PropertyInfo oPropertyInfo in oType.GetProperties())
  57. {
  58. if (oPropertyInfo.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  59. {
  60. try
  61. {
  62. oPropertyInfo.SetValue(obj, Convert.ChangeType(value, oPropertyInfo.PropertyType), null);
  63. }
  64. catch
  65. {
  66. }
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// 将对象以xml文件存储
  74. /// </summary>
  75. /// <typeparam name="T">对象类名</typeparam>
  76. /// <param name="obj">类实例</param>
  77. /// <param name="nodeName">生成的xml的根节点名称</param>
  78. /// <param name="encoding">xml的文本编码</param>
  79. public void SaveXml<T>(T obj, string nodeName, string encoding)
  80. {
  81. StringDictionary dic = new StringDictionary();
  82. Type oType = obj.GetType();
  83. foreach (PropertyInfo oPropertyInfo in oType.GetProperties())
  84. {
  85. try
  86. {
  87. object propertyValue = oPropertyInfo.GetValue(obj, null);
  88. string valueAsString = propertyValue.ToString();
  89. if (propertyValue.Equals(null))
  90. {
  91. valueAsString = String.Empty;
  92. }
  93. if (propertyValue.Equals(Int32.MinValue))
  94. {
  95. valueAsString = String.Empty;
  96. }
  97. if (propertyValue.Equals(Decimal.MinValue))
  98. {
  99. valueAsString = String.Empty;
  100. }
  101. dic.Add(oPropertyInfo.Name, valueAsString);
  102. }
  103. catch
  104. { }
  105. }
  106. SaveXml(dic, nodeName, encoding);
  107. }
  108. /// <summary>
  109. /// 将对象以xml文件存储
  110. /// </summary>
  111. /// <param name="dic">键值对象</param>
  112. /// <param name="nodeName">生成的xml的根节点名称</param>
  113. /// <param name="encoding">xml的文本编码</param>
  114. public void SaveXml(StringDictionary dic, string nodeName, string encoding)
  115. {
  116. XmlWriterSettings oXmlWriterSettings = new XmlWriterSettings();
  117. oXmlWriterSettings.Encoding = Encoding.GetEncoding(encoding);
  118. oXmlWriterSettings.Indent = true;
  119. using (XmlWriter oXmlWriter = XmlWriter.Create(xmlFilePath, oXmlWriterSettings))
  120. {
  121. oXmlWriter.WriteStartElement(nodeName);
  122. foreach (string key in dic.Keys)
  123. {
  124. oXmlWriter.WriteElementString(key, dic[key]);
  125. }
  126. oXmlWriter.WriteEndElement();
  127. }
  128. }
  129. /// <summary>
  130. /// 将对象串行化到XML
  131. /// </summary>
  132. /// <typeparam name="T"></typeparam>
  133. /// <param name="obj"></param>
  134. public void Serialize<T>(T obj)
  135. {
  136. XmlSerializer oXmlSerializer = new XmlSerializer(obj.GetType());
  137. using (Stream oStream = new FileStream(xmlFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
  138. {
  139. oXmlSerializer.Serialize(oStream, obj);
  140. }
  141. }
  142. /// <summary>
  143. /// 将XML反串行到对象
  144. /// </summary>
  145. /// <typeparam name="T"></typeparam>
  146. /// <param name="obj"></param>
  147. public void Deserialize<T>(ref T obj)
  148. {
  149. XmlSerializer oXmlSerializer = new XmlSerializer(typeof(T));
  150. using (Stream oStream = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  151. {
  152. obj = (T)oXmlSerializer.Deserialize(oStream);
  153. }
  154. }
  155. #endregion
  156. }
  157. }