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; } } }