487 lines
17 KiB
C#
487 lines
17 KiB
C#
![]() |
using Epost.Model;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Configuration;
|
|||
|
using System.Data;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
using System.Xml.Linq;
|
|||
|
using System.Xml.Serialization;
|
|||
|
|
|||
|
namespace Epost.Common
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// XmlHelper XML 操作
|
|||
|
/// </summary>
|
|||
|
public class XmlHelper
|
|||
|
{
|
|||
|
#region 获取XML 内容
|
|||
|
/// <summary>
|
|||
|
/// 获取XML 内容
|
|||
|
/// </summary>
|
|||
|
/// <param name="load">路径,XML文档</param>
|
|||
|
/// <param name="Act">1路径,2文档</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static XmlDocument GetLoadXml(string load, int Act)
|
|||
|
{
|
|||
|
XmlDocument doc = new XmlDocument();
|
|||
|
try
|
|||
|
{
|
|||
|
if (Act == 1)
|
|||
|
{
|
|||
|
doc.Load(FileHelper.GeAbsolutePath(load));
|
|||
|
}
|
|||
|
else if (Act == 2)
|
|||
|
{
|
|||
|
doc.LoadXml(load);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception er)
|
|||
|
{
|
|||
|
throw er;
|
|||
|
}
|
|||
|
return doc;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region 创建一个节点
|
|||
|
/// <summary>
|
|||
|
/// 创建一个节点 列
|
|||
|
/// XmlDocument doc = Commonly.XmlOperating.GetLoadXml(GetXML(2), 2);
|
|||
|
/// XmlNode root = doc.SelectSingleNode("bookstore");
|
|||
|
/// string[] Nodetype = { "genre", "ISBN" };
|
|||
|
/// string[] Nodetypeval = { "李赞红", "2-3631-4" };
|
|||
|
/// XmlElement xmlent = Commonly.XmlOperating.CreateNode(doc, "book", Nodetype, Nodetypeval, ""); //创建book节点
|
|||
|
/// XmlElement xmlent1 = Commonly.XmlOperating.CreateNode(doc, "title", null, null, "CS从入门到精通");
|
|||
|
/// xmlent.AppendChild(xmlent1);
|
|||
|
/// root.AppendChild(xmlent); //也可以直接添加 doc.AppendChild(xmlent)
|
|||
|
/// doc.Save(Server.MapPath("/bookstore.xml"));
|
|||
|
/// </summary>
|
|||
|
/// <param name="doc">XmlDocument</param>
|
|||
|
/// <param name="NodeName">节点名称</param>
|
|||
|
/// <param name="Nodetype">节点属性类型</param>
|
|||
|
/// <param name="NodetypeVal">节点属性值</param>
|
|||
|
/// <param name="Text">节点中的内容</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static XmlElement CreateNode(XmlDocument doc, string NodeName, string[] Nodetype, string[] NodetypeVal, string Text)
|
|||
|
{
|
|||
|
XmlElement xmlent = doc.CreateElement(NodeName); //创建节点名称
|
|||
|
try
|
|||
|
{
|
|||
|
if (Nodetype != null)
|
|||
|
{
|
|||
|
for (int i = 0; i < Nodetype.Length; i++)
|
|||
|
{
|
|||
|
xmlent.SetAttribute(Nodetype[i], NodetypeVal[i]); //添加节点属性和属性值
|
|||
|
}
|
|||
|
}
|
|||
|
if (Text != "" && Text != null)
|
|||
|
{
|
|||
|
xmlent.InnerText = Text;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception er)
|
|||
|
{
|
|||
|
throw er;
|
|||
|
}
|
|||
|
return xmlent;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region 直接通过XM类 获取内容
|
|||
|
//XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
|
|||
|
//XmlNodeList xnl = xn.ChildNodes;
|
|||
|
//foreach (XmlNode xnf in xnl)
|
|||
|
//{
|
|||
|
// XmlElement xe = (XmlElement)xnf;
|
|||
|
// Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
|
|||
|
// Console.WriteLine(xe.GetAttribute("ISBN"));
|
|||
|
// XmlNodeList xnf1 = xe.ChildNodes;
|
|||
|
// foreach (XmlNode xn2 in xnf1)
|
|||
|
// {
|
|||
|
// Console.WriteLine(xn2.InnerText);//显示子节点点文本
|
|||
|
// }
|
|||
|
//}
|
|||
|
#endregion
|
|||
|
#region 通过XML路径获取 DataSet
|
|||
|
/// <summary>
|
|||
|
/// 读取xml直接返回DataSet
|
|||
|
/// 有几个tr类型就生成几个表 外加table一个表
|
|||
|
/// 表面就是
|
|||
|
/// ds.Tables["LicenseKey"].Columns.Count// 列号
|
|||
|
/// ds.Tables["LicenseKey"].Rows[0][列号].ToString()
|
|||
|
/// </summary>
|
|||
|
/// <param name="strXmlPath">xml文件相对路径</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static DataSet GetDataSetByXml(string strXmlPath)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
DataSet ds = new DataSet();
|
|||
|
ds.ReadXml(FileHelper.GeAbsolutePath(strXmlPath));
|
|||
|
if (ds.Tables.Count > 0)
|
|||
|
{
|
|||
|
return ds;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ee)
|
|||
|
{
|
|||
|
throw new Exception(ee.Message);
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region 通过XML字符串获取 DataSeet
|
|||
|
/// <summary>
|
|||
|
/// 读取xml字符串直接返回DataSet
|
|||
|
/// 有几个tr类型就生成几个表 外加table一个表
|
|||
|
/// 表面就是
|
|||
|
/// ds.Tables["LicenseKey"].Columns.Count// 列号
|
|||
|
/// ds.Tables["LicenseKey"].Rows[0][列号].ToString()
|
|||
|
/// </summary>
|
|||
|
/// <param name="strXmlData">xml字符串</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static DataSet GetDataSetStrXml(string strXmlData)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
DataSet ds = new DataSet();
|
|||
|
using (StringReader xmlread = new StringReader(strXmlData))
|
|||
|
{
|
|||
|
ds.ReadXml(xmlread, XmlReadMode.InferTypedSchema);
|
|||
|
if (ds.Tables.Count > 0)
|
|||
|
{
|
|||
|
return ds;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ee)
|
|||
|
{
|
|||
|
throw new Exception(ee.Message);
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region DataSet 转换成 XML
|
|||
|
/// <summary>
|
|||
|
/// DataSet 转换成 XML
|
|||
|
/// </summary>
|
|||
|
/// <param name="ds"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string GetXMLData(DataSet ds)
|
|||
|
{
|
|||
|
if (ds != null)
|
|||
|
{
|
|||
|
return ds.GetXml();
|
|||
|
}
|
|||
|
return "";
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region XML节点属性修改
|
|||
|
/// <summary>
|
|||
|
/// XML节点属性修改
|
|||
|
/// </summary>
|
|||
|
/// <param name="nodelist">
|
|||
|
/// 节点集合
|
|||
|
/// XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes
|
|||
|
/// </param>
|
|||
|
/// <param name="typeName">属性名称</param>
|
|||
|
/// <param name="typeval">属性值</param>
|
|||
|
/// <param name="updatetypeval">修改的属性值</param>
|
|||
|
/// <param name="checkchild">是否修改 节点下的子节点</param>
|
|||
|
public static void UpdateXmlNodeType(XmlNodeList nodelist, string typeName, string typeval, string updatetypeval, bool checkchild)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
foreach (XmlNode xmlnode in nodelist)
|
|||
|
{
|
|||
|
XmlElement xmlent = (XmlElement)xmlnode;
|
|||
|
UpdateXmlNodeType(xmlent, typeName, typeval, updatetypeval);
|
|||
|
if (xmlent.HasChildNodes && checkchild) //如果该节点下还有节点 继续搜索
|
|||
|
{
|
|||
|
UpdateXmlNodeType(xmlent.ChildNodes, typeName, typeval, updatetypeval, checkchild);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// XML节点属性修改
|
|||
|
/// </summary>
|
|||
|
/// <param name="xmlent">节点</param>
|
|||
|
/// <param name="typeName">属性名称</param>
|
|||
|
/// <param name="typeval">属性值</param>
|
|||
|
/// <param name="updatetypeval">修改的属性值</param>
|
|||
|
public static void UpdateXmlNodeType(XmlElement xmlent, string typeName, string typeval, string updatetypeval)
|
|||
|
{
|
|||
|
if (xmlent.GetAttribute(typeName) == typeval)
|
|||
|
{
|
|||
|
xmlent.SetAttribute(typeName, updatetypeval);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region XML节点的值进行修改
|
|||
|
/// <summary>
|
|||
|
/// XML节点的值进行修改
|
|||
|
/// </summary>
|
|||
|
/// <param name="nodelist">
|
|||
|
/// 节点集合
|
|||
|
/// XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes
|
|||
|
/// </param>
|
|||
|
/// <param name="TName">节点名</param>
|
|||
|
/// <param name="updatetext">修改的值</param>
|
|||
|
/// <param name="checkchild">是否修改 节点下的子节点</param>
|
|||
|
public static void UpdateXmlNodeText(XmlNodeList nodelist, string TName, string updatetext, bool checkchild)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
foreach (XmlNode node in nodelist)
|
|||
|
{
|
|||
|
XmlElement xmlent = (XmlElement)node;
|
|||
|
UpdateXmlNodeText(xmlent, TName, updatetext);
|
|||
|
if (xmlent.HasChildNodes && checkchild) //如果该节点下还有节点 继续搜索
|
|||
|
{
|
|||
|
UpdateXmlNodeText(xmlent.ChildNodes, TName, updatetext, checkchild);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// XML节点的值进行修改
|
|||
|
/// </summary>
|
|||
|
/// <param name="xmlent">节点</param>
|
|||
|
/// <param name="TName">节点名</param>
|
|||
|
/// <param name="updatetext">修改的值</param>
|
|||
|
public static void UpdateXmlNodeText(XmlElement xmlent, string TName, string updatetext)
|
|||
|
{
|
|||
|
if (xmlent.Name == TName)
|
|||
|
{
|
|||
|
xmlent.InnerText = updatetext;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region XML节点属性删除
|
|||
|
/// <summary>
|
|||
|
/// XML节点属性删除
|
|||
|
/// </summary>
|
|||
|
/// <param name="nodelist">
|
|||
|
/// 节点集合
|
|||
|
/// XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes
|
|||
|
/// </param>
|
|||
|
/// <param name="typename">属性名</param>
|
|||
|
/// <param name="typeval">属性的值</param>
|
|||
|
public static void DelXmlNodeType(XmlNodeList nodelist, string typename, string typeval)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
foreach (XmlNode node in nodelist)
|
|||
|
{
|
|||
|
XmlElement xmlent = (XmlElement)node;
|
|||
|
DelXmlNodeType(xmlent, typename, typeval);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ee)
|
|||
|
{
|
|||
|
throw ee;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// XML节点属性删除
|
|||
|
/// </summary>
|
|||
|
/// <param name="xmlent">节点</param>
|
|||
|
/// <param name="typename">属性名称</param>
|
|||
|
/// <param name="typeval">属性值</param>
|
|||
|
public static void DelXmlNodeType(XmlElement xmlent, string typename, string typeval)
|
|||
|
{
|
|||
|
if (xmlent.GetAttribute(typename) == typeval)
|
|||
|
{
|
|||
|
xmlent.RemoveAttribute(typename);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region XML节点的全部内容删除和该节点的创建属性,不删除默认属性
|
|||
|
/// <summary>
|
|||
|
/// XML节点的全部内容删除 和该节点的创建属性,不删除默认属性
|
|||
|
/// </summary>
|
|||
|
/// <param name="nodelist">
|
|||
|
/// 节点集合
|
|||
|
/// XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes
|
|||
|
/// </param>
|
|||
|
/// <param name="Tname">节点名称</param>
|
|||
|
public static void DelXmlNodeAll(XmlNodeList nodelist, string Tname)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
foreach (XmlNode node in nodelist)
|
|||
|
{
|
|||
|
XmlElement xmlent = (XmlElement)node;
|
|||
|
DelXmlNodeAll(xmlent, Tname);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ee)
|
|||
|
{
|
|||
|
throw ee;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// XML节点的全部内容删除 和该节点的创建属性,不删除默认属性
|
|||
|
/// </summary>
|
|||
|
/// <param name="xmlent">节点</param>
|
|||
|
/// <param name="Tname">节点名</param>
|
|||
|
public static void DelXmlNodeAll(XmlElement xmlent, string Tname)
|
|||
|
{
|
|||
|
if (xmlent.Name == Tname)
|
|||
|
{
|
|||
|
xmlent.RemoveAll(); //移除内容
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
#region XML反序列化
|
|||
|
/// <summary>
|
|||
|
/// XML反序列化
|
|||
|
/// </summary>
|
|||
|
/// <param name="type">对象类型 (注意类型就是 读取节点的名称)</param>
|
|||
|
/// <param name="filename">文件路径</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static T GetDeserialization<T>(string filename)
|
|||
|
{
|
|||
|
FileStream fs = null;
|
|||
|
try
|
|||
|
{
|
|||
|
fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|||
|
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
|||
|
return (T)serializer.Deserialize(fs);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
if (fs != null)
|
|||
|
fs.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 将XML反序列化成对象
|
|||
|
/// <summary>
|
|||
|
/// 将XML反序列化成对象
|
|||
|
/// </summary>
|
|||
|
/// <typeparam name="T"></typeparam>
|
|||
|
/// <param name="source"></param>
|
|||
|
/// <param name="encoding"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static T XMLToObject<T>(string source, Encoding encoding)
|
|||
|
{
|
|||
|
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
|
|||
|
using (MemoryStream stream = new MemoryStream(encoding.GetBytes(source)))
|
|||
|
{
|
|||
|
return (T)mySerializer.Deserialize(stream);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region XML序列化
|
|||
|
/// <summary>
|
|||
|
/// XML序列化
|
|||
|
/// </summary>
|
|||
|
/// <param name="obj">对象</param>
|
|||
|
/// <param name="filename">文件路径</param>
|
|||
|
public static void SaveDeserialization(object obj, string filename)
|
|||
|
{
|
|||
|
FileStream fs = null;
|
|||
|
try
|
|||
|
{
|
|||
|
fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
|
|||
|
XmlSerializer serializer = new XmlSerializer(obj.GetType());
|
|||
|
serializer.Serialize(fs, obj);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
if (fs != null)
|
|||
|
fs.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
//#region 获取Config数据
|
|||
|
///// <summary>
|
|||
|
///// 获取Config数据
|
|||
|
///// </summary>
|
|||
|
///// <param name="xmlname">节点名称</param>
|
|||
|
///// <returns></returns>
|
|||
|
//public static string GetXmlMapPath(string xmlname)
|
|||
|
//{
|
|||
|
// return ConfigurationManager.AppSettings[xmlname].ToString();
|
|||
|
//}
|
|||
|
//#endregion
|
|||
|
|
|||
|
public List<ControlModel> ReadControlByXML(string path, string nodename)
|
|||
|
{
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename)
|
|||
|
select ele;
|
|||
|
return GetControlByElements(elements);
|
|||
|
}
|
|||
|
public List<ControlModel> GetControlByElements(IEnumerable<XElement> elements)
|
|||
|
{
|
|||
|
List<ControlModel> list = new List<ControlModel>();
|
|||
|
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
ControlModel model = new ControlModel();
|
|||
|
model.ControlName = ele.Element("ControlName").Value;
|
|||
|
model.ControlBus = ele.Element("ControlBus").Value;
|
|||
|
model.ControlIP = ele.Element("ControlIP").Value;
|
|||
|
model.ControlID = ele.FirstAttribute.Value;
|
|||
|
list.Add(model);
|
|||
|
}
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public ControlModel ReadControlByXML(string path, string nodename, string controlIP)
|
|||
|
{
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename)
|
|||
|
select ele;
|
|||
|
return GetControlByControlIP(elements, controlIP);
|
|||
|
}
|
|||
|
public ControlModel GetControlByControlIP(IEnumerable<XElement> elements, string controlIP)
|
|||
|
{
|
|||
|
|
|||
|
ControlModel model = new ControlModel();
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
if (ele.Element("ControlIP").Value == controlIP)
|
|||
|
{
|
|||
|
model.ControlName = ele.Element("ControlName").Value;
|
|||
|
model.ControlBus = ele.Element("ControlBus").Value;
|
|||
|
model.ControlIP = ele.Element("ControlIP").Value;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
return model;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|