天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
12 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
namespace QMTask.Core
{
/// <summary>
/// XML操作类
/// </summary>
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;
}
/// <summary>
/// 从XML中获取实体列表
/// </summary>
/// <param name="XmlPathNode"></param>
/// <returns></returns>
public List<T> GetData<T>(string XmlPathNode) where T : new()
{
try
{
DataTable dt = GetData(XmlPathNode);
if (dt != null)
{
return ConvertToModel<T>(dt);
}
else
{
return new List<T>();
}
}
catch
{
throw;
}
}
/// <summary>
/// //查找数据。返回一个DataView
/// </summary>
/// <param name="XmlPathNode"></param>
/// <returns></returns>
public DataTable GetData(string XmlPathNode)
{
//查找数据。返回一个DataView
DataSet ds = new DataSet();
StringBuilder str = new StringBuilder();
str.Append("<root>");
foreach (XmlNode a in objXmlDoc.SelectNodes(XmlPathNode))
{
str.Append(a.OuterXml);
str.Append("\r\n");
}
str.Append("</root>");
StringReader read = new StringReader(str.ToString());
ds.ReadXml(read);
if (ds.Tables.Count == 0)
{
return null;
}
else
{
return ds.Tables[0];
}
}
/// <summary>
/// 新节点内容。
/// 示例:xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
/// </summary>
/// <param name="XmlPathNode"></param>
/// <param name="Content"></param>
public void Replace(string XmlPathNode, string Content)
{
//更新节点内容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}
/// <summary>
/// 修改节点属性
/// </summary>
/// <param name="Node"></param>
/// <param name="attrs"></param>
public void UpdateAttributes(string Node,Dictionary<string, string> attrs)
{
if (objXmlDoc.SelectSingleNode(Node) == null)
{
return;
}
foreach (var a in attrs)
{
objXmlDoc.SelectSingleNode(Node).Attributes[a.Key].Value = a.Value;
}
}
/// <summary>
/// 删除一个指定节点的子节点。
/// 示例: xmlTool.DeleteChild("Book/Authors[ISBN=\"0003\"]");
/// </summary>
/// <param name="Node"></param>
public void DeleteChild(string Node)
{
//删除一个节点。
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
/// <summary>
/// * 使用示列:
/// 示例: XmlHelper.Delete( "/Node", "")
/// XmlHelper.Delete( "/Node", "Attribute")
/// </summary>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
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 { }
}
/// <summary>
/// 插入一节点和此节点的一子节点。
/// 示例:xmlTool.InsertNode("Book","Author","ISBN","0004");
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="ChildNode">子节点</param>
/// <param name="Element">元素</param>
/// <param name="Content">内容</param>
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);
}
/// <summary>
/// 插入一个节点,带一属性。
/// 示例: xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="Element">元素</param>
/// <param name="Attrib">属性</param>
/// <param name="AttribContent">属性内容</param>
/// <param name="Content">元素内容</param>
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);
}
/// <summary>
/// 插入一个节点,带节点列表。
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="Element">元素</param>
/// <param name="Attrib">属性</param>
/// <param name="AttribContent">属性内容</param>
/// <param name="Content">元素内容</param>
public void InsertElement(string MainNode, string Element, Dictionary<string,string> 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);
}
/// <summary>
/// 插入一个节点,不带属性。
/// 示例:xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="Element">元素</param>
/// <param name="Content">元素内容</param>
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);
}
/// <summary>
/// 对xml文件做插入,更新,删除后需做Save()操作,以保存修改
/// </summary>
public void Save()
{
//保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
/// <summary>
/// 将datatable转成实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToModel<T>(DataTable dt) where T : new()
{
try
{
List<T> lists = new List<T>();
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;
}
}
}
}