using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Xml;
using System.IO;
using ICSharpCode.Core;
using System.Reflection;
namespace Mesnac.Basic
{
#region XML操作主类
///
/// XML解析类
///
public class XmlHandler
{
#region 平台设计环境下对XML操作的主要方法
///
/// 解析工程向导文件
///
/// 工程向导文件
///
public static Dictionary ParseFromProjectWizardXml(string fileName)
{
Dictionary dic = new Dictionary();
if (File.Exists(fileName))
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodeListProjectWizard = doc.GetElementsByTagName("ProjectWizard");
ProjectWizard projectWizard = null;
foreach (XmlNode node in nodeListProjectWizard)
{
projectWizard = new ProjectWizard();
projectWizard.Name = node.Attributes["Name"].Value;
projectWizard.EnglishName = node.Attributes["EnglishName"].Value;
projectWizard.EnglishShortName = node.Attributes["EnglishShortName"].Value;
projectWizard.ImageIndex = String.IsNullOrWhiteSpace(node.Attributes["ImageIndex"].Value) ? -1 : Convert.ToInt32(node.Attributes["ImageIndex"].Value);
projectWizard.Description = node.Attributes["Description"].Value;
projectWizard.EnglishDescription = node.Attributes["EnglishDescription"].Value;
dic.Add(projectWizard.Name, projectWizard);
}
}
return dic;
}
///
/// 解析数据源文件
///
/// 数据源文件
/// 数据源集合
public static Dictionary ParseFromDataSourceXml(string fileName)
{
Dictionary dic = new Dictionary();
if (File.Exists(fileName))
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodeListDataSource = doc.GetElementsByTagName("DataSourceItem");
DataSourceItem dataSourceItem = null;
foreach (XmlNode node in nodeListDataSource)
{
dataSourceItem = new DataSourceItem();
dataSourceItem.Name = node.Attributes["Name"].Value;
dataSourceItem.Driver = node.Attributes["Driver"].Value;
dataSourceItem.Server = node.Attributes["Server"].Value;
dataSourceItem.UserName = node.Attributes["UserName"].Value;
dataSourceItem.Password = node.Attributes["Password"].Value;
dataSourceItem.Database = node.Attributes["DataBase"].Value;
dataSourceItem.ConnectionTimeout = GetAttributeIntValue(node, "ConnectionTimeout", 5); //默认连接超时
dataSourceItem.DriverAssembly = node.Attributes["DriverAssembly"].Value;
dataSourceItem.DriverClass = node.Attributes["DriverClass"].Value;
dataSourceItem.DataSourceClass = node.Attributes["DataSourceClass"].Value;
dic.Add(dataSourceItem.Name, dataSourceItem);
}
}
return dic;
}
///
/// 生成数据源配置文件
///
/// 数据源集合
/// 数据源文件
public static void GenerateDataSourceXml(Dictionary dic, string fileName)
{
XmlDocument doc = new XmlDocument();
XmlElement eroot = doc.CreateElement("DataSources");
foreach (DataSourceItem dataSourceItem in dic.Values)
{
XmlElement eDataSourceItem = doc.CreateElement("DataSourceItem");
XmlAttribute attName = doc.CreateAttribute("Name");
attName.Value = dataSourceItem.Name;
eDataSourceItem.Attributes.Append(attName);
XmlAttribute attDriver = doc.CreateAttribute("Driver");
attDriver.Value = dataSourceItem.Driver;
eDataSourceItem.Attributes.Append(attDriver);
XmlAttribute attServer = doc.CreateAttribute("Server");
attServer.Value = dataSourceItem.Server;
eDataSourceItem.Attributes.Append(attServer);
XmlAttribute attUserName = doc.CreateAttribute("UserName");
attUserName.Value = dataSourceItem.UserName;
eDataSourceItem.Attributes.Append(attUserName);
XmlAttribute attPassword = doc.CreateAttribute("Password");
attPassword.Value = dataSourceItem.Password;
eDataSourceItem.Attributes.Append(attPassword);
XmlAttribute attDataBase = doc.CreateAttribute("DataBase");
attDataBase.Value = dataSourceItem.Database;
eDataSourceItem.Attributes.Append(attDataBase);
XmlAttribute attConnectionTimeout = doc.CreateAttribute("ConnectionTimeout");
attConnectionTimeout.Value = dataSourceItem.ConnectionTimeout.ToString();
eDataSourceItem.Attributes.Append(attConnectionTimeout);
XmlAttribute attDriverAssembly = doc.CreateAttribute("DriverAssembly");
attDriverAssembly.Value = dataSourceItem.DriverAssembly;
eDataSourceItem.Attributes.Append(attDriverAssembly);
XmlAttribute attDriverClass = doc.CreateAttribute("DriverClass");
attDriverClass.Value = dataSourceItem.DriverClass;
eDataSourceItem.Attributes.Append(attDriverClass);
XmlAttribute attDataSourceClass = doc.CreateAttribute("DataSourceClass");
attDataSourceClass.Value = dataSourceItem.DataSourceClass;
eDataSourceItem.Attributes.Append(attDataSourceClass);
eroot.AppendChild(eDataSourceItem);
}
doc.AppendChild(eroot);
doc.Save(fileName);
}
///
/// 解析画面文件
///
/// 画面文件名
///
public static Dictionary ParseFormXml(string fileName)
{
Dictionary dic = new Dictionary();
if (File.Exists(fileName))
{
XmlDocument doc = ReadXmlFile(fileName);
XmlNodeList nodeListObject = doc.GetElementsByTagName("Object");
ObjectDescriptor objectDescriptor = null;
foreach (XmlNode node1 in nodeListObject)
{
objectDescriptor = new ObjectDescriptor();
objectDescriptor.Name = node1.Attributes["name"].Value;
if (dic.ContainsKey(objectDescriptor.Name))
{
continue;
}
if (node1.ParentNode != node1.OwnerDocument && node1.ParentNode.Attributes["name"] != null)
{
objectDescriptor.ParentName = node1.ParentNode.Attributes["name"].Value;
}
XmlNodeList nodeListProperty = node1.ChildNodes;
foreach (XmlNode node2 in nodeListProperty)
{
if (node2.Name == "Property")
{
objectDescriptor.Properties.Add(node2.Attributes["name"].Value);
}
}
dic.Add(node1.Attributes["name"].Value, objectDescriptor);
}
System.GC.Collect(); //回收内存
}
return dic;
}
#endregion
#region 平台运行环境下对XML操作的主要方法
///
/// 获取组态工程目录下所有的DLL集合
///
/// 组态工程目录(.mprj文件所在目录)
/// 返回DLL集合
public static Dictionary GetProjectDLLNode(string mcProjectPath)
{
Dictionary formsDic = new Dictionary();
string formListPath = mcProjectPath + "\\extend\\";
if (Directory.Exists(formListPath))
{
DirectoryInfo formsDir = new DirectoryInfo(formListPath);
foreach (FileInfo item in formsDir.GetFiles("*.dll"))
{
formsDic.Add(Path.GetFileNameWithoutExtension(item.Name), GetFormTextFromDLL(Path.GetFileNameWithoutExtension(item.Name),item.FullName));
}
}
return formsDic;
}
///
/// 获取组态工程目录下所有的画面集合
///
/// 组态工程目录(.mprj文件所在目录)
/// 返回画面集合
public static Dictionary GetProjectFormNode(string mcProjectPath)
{
Dictionary formsDic = new Dictionary();
string formListPath = Path.Combine(mcProjectPath, FixNodeName.NodeFormName);
if (Directory.Exists(formListPath))
{
DirectoryInfo formsDir = new DirectoryInfo(formListPath);
foreach (FileInfo item in formsDir.GetFiles("*.xml",SearchOption.TopDirectoryOnly))
{
formsDic.Add(Path.GetFileNameWithoutExtension(item.Name), GetFormText(item.FullName));
}
}
return formsDic;
}
///
/// 获取工程命令列表
///
/// 工程路径
/// 返回工程命令集合
public static Dictionary> GetProjectCommands(string mcProjectPath)
{
Dictionary> commandsDic = new Dictionary>();
string commandListPath = Path.Combine(mcProjectPath, FixNodeName.NodeCommandName + ".xml");
if (File.Exists(commandListPath))
{
XmlDocument doc = new XmlDocument();
doc.Load(commandListPath);
XmlNodeList nodeList = doc.GetElementsByTagName("Property");
List valueList = null;
foreach (XmlNode node in nodeList)
{
string key = node.Attributes["actionname"].Value;
if (!commandsDic.ContainsKey(key))
{
valueList = new List();
valueList.Add(node.Attributes["guid"].Value);
commandsDic.Add(key, valueList);
}
else
{
commandsDic[key].Add(node.Attributes["guid"].Value);
}
}
}
return commandsDic;
}
///
/// 获取组态工程目录下所有的画面菜单集合
///
/// 组态工程目录(.mprj文件所在目录)
/// 返回画面菜单集合
public static List GetProjectFormMenus(string mcProjectPath)
{
Dictionary formsDic = GetProjectFormNode(mcProjectPath);
List lst = new List();
RunConfigMenuItem menuItem = null;
foreach (string key in formsDic.Keys)
{
menuItem = new RunConfigMenuItem();
menuItem.FormFile = key;
menuItem.ID = key;
menuItem.FormText = formsDic[key];
menuItem.Text = formsDic[key];
menuItem.IsSystem = false;
lst.Add(menuItem);
}
Dictionary> commandsDic = GetProjectCommands(mcProjectPath);
foreach (string key in commandsDic.Keys)
{
menuItem = new RunConfigMenuItem();
menuItem.ID = "Command_" + key;
menuItem.FormText = key;
menuItem.Text = key;
menuItem.CreateType = CreateType.Command;
menuItem.CommandValue = commandsDic[key];
menuItem.IsSystem = false;
lst.Add(menuItem);
}
Dictionary formsDll = GetProjectDLLNode(mcProjectPath);
foreach (string key in formsDll.Keys)
{
menuItem = new RunConfigMenuItem();
menuItem.FormFile = key;
menuItem.ID = "dll_" + key;
menuItem.FormText = formsDll[key];
menuItem.Text = formsDll[key];
menuItem.IsSystem = false;
lst.Add(menuItem);
}
return lst;
}
///
/// 获取画面文件中窗体的Text属性值
///
///
///
private static string GetFormText(string fileName)
{
string returnstr = String.Empty;
XmlDocument doc = ReadXmlFile(fileName);
XmlNode formNode = doc.SelectSingleNode("DOCUMENT_ELEMENT/Object");
foreach (XmlNode item in formNode.ChildNodes)
{
if (item.Name == "Property" && item.Attributes["name"].InnerText == "Text")
{
returnstr = item.InnerText;
}
}
return returnstr;
}
///
/// 获取DLL文件中窗体的FormName属性值
///
/// 不带扩展名的文件名称
/// 文件全路径名称
///
private static string GetFormTextFromDLL(string fileName,string fileFullName)
{
string returnstr = String.Empty;
Assembly ass = Assembly.LoadFrom(fileFullName);
Type ob = ass.GetType(fileName + ".App");
if (ob.IsClass)
{
object obj = (object)Activator.CreateInstance(ob);
FieldInfo[] fieldInfo = ob.GetFields();
for (int i = 0; i < fieldInfo.Length; i++)
{
if(fieldInfo[i].Name == "FormName")
{
returnstr = fieldInfo[i].GetValue(obj).ToString();
break;
}
}
}
return returnstr;
}
#endregion
#region 通用辅助方法
///
/// 加载画面文件,把XML文件加载为XmlDocument对象
///
/// 画面文件名
/// 返回加载后的XmlDocument对象
private static XmlDocument ReadXmlFile(string fileName)
{
try
{
StreamReader sr = new StreamReader(fileName);
string cleandown = sr.ReadToEnd();
sr.Close();
cleandown = "" + cleandown + "";
XmlDocument doc = new XmlDocument();
doc.LoadXml(cleandown);
return doc;
}
catch (Exception ex)
{
Console.Write(ex.Message);
throw ex;
}
}
///
/// 获取XML文档那个节点字符串属性值
///
/// XML文档节点对象
/// 属性名称
/// 如果获取失败,返回此默认值
/// 返回对应的节点值
public static string GetAttributeStringValue(XmlNode node, string attributeName, string defaultValue)
{
if (node.Attributes[attributeName] != null)
{
return node.Attributes[attributeName].Value;
}
else
{
return defaultValue;
}
}
///
/// 获取XML文档那个节点整型属性值
///
/// XML文档节点对象
/// 属性名称
/// 如果获取失败,返回此默认值
/// 返回对应的节点值
public static int GetAttributeIntValue(XmlNode node, string attributeName, int defaultValue)
{
if (node.Attributes[attributeName] != null)
{
int.TryParse(node.Attributes[attributeName].Value, out defaultValue);
}
return defaultValue;
}
///
/// 获取XML文档那个节点布尔属性值
///
/// XML文档节点对象
/// 属性名称
/// 如果获取失败,返回此默认值
/// 返回对应的节点值
public static bool GetAttributeBoolValue(XmlNode node, string attributeName, bool defaultValue)
{
if (node.Attributes[attributeName] != null)
{
bool.TryParse(node.Attributes[attributeName].Value, out defaultValue);
}
return defaultValue;
}
#endregion
}
#endregion
#region XML操作中用到的实体类定义
#region 工程向导实体类,对应工程向导配置文件中的一条配置项
///
/// 工程向导实体类,对应工程向导配置文件中的一条配置项
///
[Serializable]
public class ProjectWizard
{
///
/// 工程向导中文名称
///
public string Name { get; set; }
///
/// 工程向导英文名称
///
public string EnglishName { get; set; }
///
/// 工程向导英文名称缩写
///
public string EnglishShortName { get; set; }
///
/// 工程向导对应的图标索引
///
public int ImageIndex { get; set; }
///
/// 中文描述信息
///
public string Description { get; set; }
///
/// 英文描述信息
///
public string EnglishDescription { get; set; }
}
#endregion
#region 画面文件中的对象描述器实体类,对应Form.xml中的Object元素
///
/// 画面文件中的对象描述器实体类,对应Form.xml中的Object元素
///
[Serializable]
public class ObjectDescriptor
{
private string parentName;
private string name;
private List properties = new List();
public ObjectDescriptor()
{
}
///
/// 父级对象名称
///
public string ParentName
{
get { return parentName; }
set { parentName = value; }
}
///
/// 对象名称
///
public string Name
{
get { return name; }
set { name = value; }
}
///
/// 对象属性名称列表
///
public List Properties
{
get { return properties; }
set { properties = value; }
}
}
#endregion
#region 画面对应的菜单项实体
///
/// 画面对应的菜单项实体
///
[Serializable]
public class RunConfigMenuItem
{
#region 字段定义
private string _ID = String.Empty;
private string _msgId = String.Empty;
private string _text = String.Empty;
private string _formFile = String.Empty;
private string _formText = String.Empty;
private string _icon = String.Empty;
private Image _img = null;
private int _index = 0;
private string _shortCut = String.Empty;
private FormShowType _showType = FormShowType.Document;
private CreateType _createType = CreateType.Form;
private bool _isSystem = false;
private bool _isAutoLoad = false;
private RunConfigMenuItem _parent = null;
private List _commandValue = null;
#endregion
///
/// 标识
///
public string ID
{
get { return this._ID; }
set { this._ID = value; }
}
///
/// 国际化资源ID
///
public string MsgId
{
get { return _msgId; }
set { _msgId = value; }
}
///
/// 显示文本
///
public string Text
{
get { return this._text; }
set { this._text = value; }
}
///
/// 画面文件名
///
public string FormFile
{
get { return this._formFile; }
set { this._formFile = value; }
}
///
/// 原始画面窗体Text属性
///
public string FormText
{
get { return this._formText; }
set { this._formText = value; }
}
///
/// 图标文件路径
///
public string ICON
{
get { return this._icon; }
set { this._icon = value; }
}
///
/// 图标对象
///
public Image Img
{
get { return this._img; }
set { this._img = value; }
}
///
/// 索引位置
///
public int Index
{
get { return this._index; }
set { this._index = value; }
}
///
/// 快捷键文本
///
public string ShortCut
{
get { return this._shortCut; }
set { this._shortCut = value; }
}
///
/// 是否已对话框显示
///
public FormShowType ShowType
{
get { return this._showType; }
set { this._showType = value; }
}
///
/// 菜单的创建类型
///
public CreateType CreateType
{
get { return this._createType; }
set { this._createType = value; }
}
///
/// 是否为系统菜单
///
public bool IsSystem
{
get { return this._isSystem; }
set { this._isSystem = value; }
}
///
/// 是否自动加载
///
public bool IsAutoLoad
{
get { return this._isAutoLoad; }
set { this._isAutoLoad = value; }
}
///
/// 父级菜单对象
///
public RunConfigMenuItem Parent
{
get { return this._parent; }
set { this._parent = value; }
}
public List CommandValue
{
get { return this._commandValue; }
set { this._commandValue = value; }
}
public override string ToString()
{
if (this._createType == CreateType.Form)
{
if (String.IsNullOrEmpty(this.FormFile))
{
return String.Format("{0}", this._text);
}
else if (this._ID.Contains("dll_"))
{
return String.Format("{0}({1}.dll)", this._text, this.FormFile);
}
else
{
return String.Format("{0}({1}.xml)", this._text, this.FormFile);
}
}
else if (this._createType == CreateType.Command)
{
return String.Format("{0}", this._text);
}
else
{
return String.Empty;
}
}
}
///
/// 菜单命令调用窗体时,窗体的显示方式
///
[Serializable]
public enum FormShowType
{
///
/// 文档方式,放入DockPanel
///
Document = 1,
///
/// 独立窗体方式
///
Form = 2,
///
/// 对话框方式
///
Dialog = 3,
///
/// 多屏方式
///
MultiScreen = 4,
///
/// 浮动
///
FloatWindow = 5
}
#endregion
#endregion
///
/// Xml辅助类
///
public static class XmlHelper
{
///
/// 扩展方法-判断2个节点的Name属性值是否相等
///
/// 当前节点
/// 要比较的节点
/// 相等返回true,否则返回false
public static bool NodeEquals(this XmlNode node1, XmlNode node2)
{
if (node1.Attributes["Name"] != null && node2.Attributes["Name"] != null)
{
return node1.Attributes["Name"].Value.Equals(node2.Attributes["Name"].Value);
}
return node1.Equals(node2);
}
///
/// 节点集合中是否包含此节点
///
///
///
///
public static bool ContainsNode(this List nodeList, XmlNode node)
{
foreach (XmlNode n in nodeList)
{
if (n.NodeEquals(node))
{
return true;
}
}
return false;
}
///
/// 扩展方法-从节点集合中删除一个节点
///
/// 当前节点集合
/// 要移除的节点
public static void Remove(this List nodeList, XmlNode node)
{
foreach (XmlNode n in nodeList)
{
if (n.NodeEquals(node))
{
nodeList.Remove(n);
break;
}
}
}
///
/// 集合转换
///
/// 要转换的XmlNodeList对象
/// 变为XmlNode集合
public static List ConvertListFromNodeList(XmlNodeList nodeList)
{
List lst = new List();
foreach (XmlNode node in nodeList)
{
lst.Add(node);
}
return lst;
}
///
/// 求2个集合的交集
///
///
///
///
public static List IntersectNode(this List list1, List list2)
{
List list = new List();
foreach (XmlNode node in list1)
{
if (list2.ContainsNode(node))
{
list.Add(node);
}
}
return list;
}
///
/// 获取一组控件的属性值
///
///
///
///
public static object GetPropertyValue(object[] comps, string propertyName)
{
object value = String.Empty;
bool beginFlag = true;
foreach (object obj in comps)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName);
if (propertyInfo == null)
{
return String.Empty;
}
if (beginFlag)
{
value = propertyInfo.GetValue(obj, null);
beginFlag = false;
}
else
{
if (value == propertyInfo.GetValue(obj, null))
{
continue;
}
else
{
return String.Empty;
}
}
}
return value;
}
///
/// 为一组控件的属性赋值
///
///
///
///
public static void SetPropertyValue(object[] comps, string propertyName, object value)
{
foreach (object obj in comps)
{
System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName);
if (propertyInfo == null)
{
continue;
}
propertyInfo.SetValue(obj, value, null);
}
}
}
}