123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Reflection;
- using System.Collections.Specialized;
- using System.Xml.Serialization;
- using System.IO;
- namespace Ant.Frame
- {
- //XML操作工具类代码
- /// <summary>
- /// xml操作工具类
- /// </summary>
- public class XmlUtils
- {
- #region Fields
- /// <summary>
- /// xml文件物理地址
- /// </summary>
- private string xmlFilePath = String.Empty;
- #endregion
- #region Constructor
- /// <summary>
- /// 构造
- /// </summary>
- public XmlUtils()
- {
- }
- /// <summary>
- /// 构造
- /// </summary>
- /// <param name="path">xml文件物理地址</param>
- public XmlUtils(string path)
- {
- xmlFilePath = path;
- }
- #endregion
- #region Methods
- /// <summary>
- /// 读取xml数据,并转换成对象
- /// </summary>
- /// <typeparam name="T">对象类名</typeparam>
- /// <param name="obj">类实例对象</param>
- /// <param name="nodeName">节点名称</param>
- public void LoadXml<T>(ref T obj, string nodeName)
- {
- Type oType = obj.GetType();
- XmlDocument oXmlDocument = new XmlDocument();
- oXmlDocument.Load(xmlFilePath);
- foreach (XmlNode oXmlNode in oXmlDocument.SelectSingleNode(nodeName).ChildNodes)
- {
- string name = oXmlNode.Name;
- string value = oXmlNode.InnerText;
- foreach (PropertyInfo oPropertyInfo in oType.GetProperties())
- {
- if (oPropertyInfo.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
- {
- try
- {
- oPropertyInfo.SetValue(obj, Convert.ChangeType(value, oPropertyInfo.PropertyType), null);
- }
- catch
- {
- }
- break;
- }
- }
- }
- }
- /// <summary>
- /// 将对象以xml文件存储
- /// </summary>
- /// <typeparam name="T">对象类名</typeparam>
- /// <param name="obj">类实例</param>
- /// <param name="nodeName">生成的xml的根节点名称</param>
- /// <param name="encoding">xml的文本编码</param>
- public void SaveXml<T>(T obj, string nodeName, string encoding)
- {
- StringDictionary dic = new StringDictionary();
- Type oType = obj.GetType();
- foreach (PropertyInfo oPropertyInfo in oType.GetProperties())
- {
- try
- {
- object propertyValue = oPropertyInfo.GetValue(obj, null);
- string valueAsString = propertyValue.ToString();
- if (propertyValue.Equals(null))
- {
- valueAsString = String.Empty;
- }
- if (propertyValue.Equals(Int32.MinValue))
- {
- valueAsString = String.Empty;
- }
- if (propertyValue.Equals(Decimal.MinValue))
- {
- valueAsString = String.Empty;
- }
- dic.Add(oPropertyInfo.Name, valueAsString);
- }
- catch
- { }
- }
- SaveXml(dic, nodeName, encoding);
- }
- /// <summary>
- /// 将对象以xml文件存储
- /// </summary>
- /// <param name="dic">键值对象</param>
- /// <param name="nodeName">生成的xml的根节点名称</param>
- /// <param name="encoding">xml的文本编码</param>
- public void SaveXml(StringDictionary dic, string nodeName, string encoding)
- {
- XmlWriterSettings oXmlWriterSettings = new XmlWriterSettings();
- oXmlWriterSettings.Encoding = Encoding.GetEncoding(encoding);
- oXmlWriterSettings.Indent = true;
- using (XmlWriter oXmlWriter = XmlWriter.Create(xmlFilePath, oXmlWriterSettings))
- {
- oXmlWriter.WriteStartElement(nodeName);
- foreach (string key in dic.Keys)
- {
- oXmlWriter.WriteElementString(key, dic[key]);
- }
- oXmlWriter.WriteEndElement();
- }
- }
- /// <summary>
- /// 将对象串行化到XML
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- public void Serialize<T>(T obj)
- {
- XmlSerializer oXmlSerializer = new XmlSerializer(obj.GetType());
- using (Stream oStream = new FileStream(xmlFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
- {
- oXmlSerializer.Serialize(oStream, obj);
- }
- }
- /// <summary>
- /// 将XML反串行到对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- public void Deserialize<T>(ref T obj)
- {
- XmlSerializer oXmlSerializer = new XmlSerializer(typeof(T));
- using (Stream oStream = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- obj = (T)oXmlSerializer.Deserialize(oStream);
- }
- }
- #endregion
- }
- }
|