| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using System;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml;
- namespace Core.StlMes.Client.LgCommon
- {
- public class MyXml
- {
- public static XmlDocument GetXmlDoc(string FileName)
- {
- try
- {
- XmlDocument xmldoc = new XmlDocument();
- if (!File.Exists(FileName))
- {
- CreateXmlDeclaration(xmldoc, "1.0", "utf-8");
- }
- else
- {
- xmldoc.Load(FileName);
- }
- return xmldoc;
- }
- catch (Exception ex)
- {
- MessageBox.Show("读取XML失败!\n" + "文件名:" + FileName + "\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return null;
- }
- }
- public static void CreateXmlDeclaration(XmlDocument xmldoc, string Version, string encoding)
- {
- try
- {
- if (xmldoc != null)
- {
- XmlNodeList nodelist = xmldoc.ChildNodes;
- if (nodelist != null)
- {
- foreach (XmlNode xn in nodelist)
- {
- if (xn.NodeType == XmlNodeType.XmlDeclaration)
- {
- return;
- }
- }
- }
- XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
- xmldoc.AppendChild(xmlnode);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("XML创建声明失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- public static XmlNode GetXmlRoot(XmlDocument xmldoc)
- {
- try
- {
- if (xmldoc != null)
- {
- return xmldoc.DocumentElement;
- }
- else
- {
- MessageBox.Show("XmlDocument为空对象!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return null;
- }
- catch (Exception ex)
- {
- MessageBox.Show("根节点获取失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return null;
- }
- }
- public static XmlNode CreateXmlRoot(XmlDocument xmldoc, string rootName)
- {
- XmlNode root = GetXmlRoot(xmldoc);
- if (root != null && root.Name == rootName)
- {
- return root;
- }
- try
- {
- if (xmldoc != null)
- {
- if (root != null && root.Name != rootName)
- {
- xmldoc.RemoveChild(root);
- }
- root = xmldoc.CreateNode(XmlNodeType.Element, rootName, null);
- xmldoc.AppendChild(root);
- return root;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("根节点设置失败!" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return null;
- }
- public static XmlNode CreateXmlNode(XmlDocument xmldoc, XmlNode parent, string NodeName, string xmlnodeAttribute, string xmlnodeAttributeValue)
- {
- try
- {
- if (xmldoc != null && parent != null)
- {
- XmlNodeList nodelist = parent.ChildNodes;
- if (nodelist != null)
- {
- foreach (XmlNode xn in nodelist)
- {
- if (xn.Name == NodeName)
- {
- foreach (XmlAttribute attribute in xn.Attributes)
- {
- if (attribute.Name == xmlnodeAttribute &&
- attribute.Value == xmlnodeAttributeValue)
- {
- return xn;
- }
- }
- }
- }
- }
- XmlNode xmlnode = xmldoc.CreateElement(NodeName);
- xmlnode.Attributes.Append(xmldoc.CreateAttribute(xmlnodeAttribute)).Value = xmlnodeAttributeValue;
- parent.AppendChild(xmlnode);
- return xmlnode;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("创建XML节点【" + NodeName + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return null;
- }
- public static XmlNode AddXmlNode(XmlDocument xmldoc, XmlNode parent, string NodeName, bool canRepeated)
- {
- try
- {
- if (xmldoc != null && parent != null)
- {
- if (!canRepeated)
- {
- XmlNodeList nodelist = parent.ChildNodes;
- if (nodelist != null)
- {
- foreach (XmlNode xn in nodelist)
- {
- if (xn.Name == NodeName)
- {
- return xn;
- }
- }
- }
- }
- XmlNode xmlnode = xmldoc.CreateElement(NodeName);
- parent.AppendChild(xmlnode);
- return xmlnode;
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("新增XML节点【" + NodeName + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return null;
- }
- public static void AddXmlNodeAttribute(XmlDocument xmldoc, XmlNode xmlnode, string xmlnodeAttribute, string xmlnodeAttributeValue, bool canRepeated)
- {
- try
- {
- if (xmldoc != null && xmlnode != null)
- {
- if (!canRepeated)
- {
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttribute)
- {
- return;
- }
- }
- }
- XmlAttribute node = xmldoc.CreateAttribute(xmlnodeAttribute);
- node.Value = xmlnodeAttributeValue;
- xmlnode.Attributes.Append(node);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("节点新增属性【" + xmlnodeAttribute + "】失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- public static void SetXmlNodeAttributeValue(XmlNode xmlnode, string xmlnodeAttribute, string xmlnodeAttributeValue)
- {
- try
- {
- if (xmlnode != null)
- {
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttribute)
- {
- attribute.Value = xmlnodeAttributeValue;
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("设置节点属性 " + xmlnodeAttribute + " = " + xmlnodeAttributeValue + " 失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- public static string GetXmlNodeAttributeValue(XmlDocument xmldoc, string xpath, string NodeName, string xmlnodeAttributeWhen, string xmlnodeAttributeValueWhen, string xmlnodeAttributeThen)
- {
- try
- {
- if (xmldoc != null)
- {
- XmlNodeList nodelist = xmldoc.SelectNodes(xpath);
- if (nodelist != null)
- {
- bool OK = false;
- foreach (XmlNode xmlnode in nodelist)
- {
- if (xmlnode.Name == NodeName)
- {
- OK = false;
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttributeWhen && attribute.Value == xmlnodeAttributeValueWhen)
- {
- OK = true;
- break;
- }
- }
- if (OK)
- {
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttributeThen)
- {
- return attribute.Value;
- }
- }
- }
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("获取节点属性 " + xmlnodeAttributeThen + " 值失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return "";
- }
- public static bool SetXmlNodeAttributeValue(XmlDocument xmldoc, string xpath, string NodeName, string xmlnodeAttributeWhen, string xmlnodeAttributeValueWhen, string xmlnodeAttributeThen, string xmlnodeAttributeValueThen)
- {
- try
- {
- if (xmldoc != null)
- {
- XmlNodeList nodelist = xmldoc.SelectNodes(xpath);
- if (nodelist != null)
- {
- int iDone = 0;
- bool OK = false;
- foreach (XmlNode xmlnode in nodelist)
- {
- if (xmlnode.Name == NodeName)
- {
- OK = false;
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttributeWhen && attribute.Value == xmlnodeAttributeValueWhen)
- {
- OK = true;
- break;
- }
- }
- if (OK)
- {
- foreach (XmlAttribute attribute in xmlnode.Attributes)
- {
- if (attribute.Name == xmlnodeAttributeThen)
- {
- attribute.Value = xmlnodeAttributeValueThen;
- iDone++;
- }
- }
- }
- }
- }
- if (iDone > 0)
- {
- return true;
- }
- else
- {
- MessageBox.Show("配置文件不存在属性 " + xmlnodeAttributeWhen + " = " + xmlnodeAttributeValueWhen + " !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return false;
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("设置节点属性 " + xmlnodeAttributeThen + " = " + xmlnodeAttributeValueThen + " 失败!\n" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- return false;
- }
- }
- }
|