using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
namespace QMTask.Core
{
///
/// XML操作类
///
public class XmlHelper
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();
public XmlHelper(string XmlFile)
{
//
// TODO: 在这里加入建构函式的程序代码
//
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
///
/// 从XML中获取实体列表
///
///
///
public List GetData(string XmlPathNode) where T : new()
{
try
{
DataTable dt = GetData(XmlPathNode);
if (dt != null)
{
return ConvertToModel(dt);
}
else
{
return new List();
}
}
catch
{
throw;
}
}
///
/// //查找数据。返回一个DataView
///
///
///
public DataTable GetData(string XmlPathNode)
{
//查找数据。返回一个DataView
DataSet ds = new DataSet();
StringBuilder str = new StringBuilder();
str.Append("");
foreach (XmlNode a in objXmlDoc.SelectNodes(XmlPathNode))
{
str.Append(a.OuterXml);
str.Append("\r\n");
}
str.Append("");
StringReader read = new StringReader(str.ToString());
ds.ReadXml(read);
if (ds.Tables.Count == 0)
{
return null;
}
else
{
return ds.Tables[0];
}
}
///
/// 新节点内容。
/// 示例:xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
///
///
///
public void Replace(string XmlPathNode, string Content)
{
//更新节点内容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}
///
/// 修改节点属性
///
///
///
public void UpdateAttributes(string Node,Dictionary attrs)
{
if (objXmlDoc.SelectSingleNode(Node) == null)
{
return;
}
foreach (var a in attrs)
{
objXmlDoc.SelectSingleNode(Node).Attributes[a.Key].Value = a.Value;
}
}
///
/// 删除一个指定节点的子节点。
/// 示例: xmlTool.DeleteChild("Book/Authors[ISBN=\"0003\"]");
///
///
public void DeleteChild(string Node)
{
//删除一个节点。
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
///
/// * 使用示列:
/// 示例: XmlHelper.Delete( "/Node", "")
/// XmlHelper.Delete( "/Node", "Attribute")
///
/// 节点
/// 属性名,非空时删除该节点属性值,否则删除节点值
public void Delete(string node, string attribute)
{
try
{
XmlNode xn = objXmlDoc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xn.ParentNode.RemoveChild(xn);
else
xe.RemoveAttribute(attribute);
}
catch { }
}
///
/// 插入一节点和此节点的一子节点。
/// 示例:xmlTool.InsertNode("Book","Author","ISBN","0004");
///
/// 主节点
/// 子节点
/// 元素
/// 内容
public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
{
//插入一节点和此节点的一子节点。
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}
///
/// 插入一个节点,带一属性。
/// 示例: xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
///
/// 主节点
/// 元素
/// 属性
/// 属性内容
/// 元素内容
public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
//插入一个节点,带一属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
///
/// 插入一个节点,带节点列表。
///
/// 主节点
/// 元素
/// 属性
/// 属性内容
/// 元素内容
public void InsertElement(string MainNode, string Element, Dictionary Attribs , string Content)
{
//插入一个节点,带一属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
foreach (var a in Attribs)
{
objElement.SetAttribute(a.Key, a.Value);
}
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
///
/// 插入一个节点,不带属性。
/// 示例:xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
///
/// 主节点
/// 元素
/// 元素内容
public void InsertElement(string MainNode, string Element, string Content)
{
//插入一个节点,不带属性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
///
/// 对xml文件做插入,更新,删除后需做Save()操作,以保存修改
///
public void Save()
{
//保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
///
/// 将datatable转成实体
///
///
///
///
public static List ConvertToModel(DataTable dt) where T : new()
{
try
{
List lists = new List();
Type type = typeof(T);
foreach (DataRow dr in dt.Rows)
{
T result = new T();
foreach (var item in type.GetProperties())
{
//object[] attrs = item.GetCustomAttributes(typeof(DBColumnAttribute), true);
//if (attrs.Count() == 0)
//{
// continue;
//}
//DBColumnAttribute temp = (DBColumnAttribute)attrs[0];
var column = item.Name.ToUpper();
if (!dt.Columns.Contains(column))
{ continue; }
var value = dr[column] != null ? dr[column] : null;
if (item.PropertyType == typeof(Int32))
{
int num = 0;
try
{
num = Convert.ToInt32(value);
}
catch
{
}
item.SetValue(result, num, null);
}
else if (item.PropertyType == typeof(Int64))
{
Int64 num = 0;
try
{
num = Convert.ToInt64(value);
}
catch
{
}
item.SetValue(result, num, null);
}
else if (item.PropertyType == typeof(decimal))
{
decimal num = 0;
try
{
num = Convert.ToDecimal(value);
}
catch
{
}
item.SetValue(result, num, null);
}
else if (item.PropertyType == typeof(DateTime))
{
DateTime num = new DateTime();
try
{
num = Convert.ToDateTime(value);
}
catch
{
}
item.SetValue(result, num, null);
}
else if (item.PropertyType == typeof(string))
{
item.SetValue(result, value.ToString(), null);
}
else if (value == System.DBNull.Value)
{
item.SetValue(result, "", null);
}
else
{
//object value = value == System.DBNull.Value ? null : row[i];
item.SetValue(result, value, null);
}
//item.SetValue(result, value, null);
}
lists.Add(result);
}
return lists;
}
catch (Exception ex)
{
throw;
}
}
}
}