/// <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
}