/// <summary> /// XML帮助类 /// </summary> public class XMLHelper { public XmlDocument xmlDoc { get; set; } public XMLHelper() { this.xmlDoc = new XmlDocument(); } #region Document /// <summary> /// 创建一个XML文档 /// </summary> /// <param name="rootNodeName">XML文档根节点名称(须指定一个根节点名称)</param> /// <param name="version">XML文档版本号(必须为:"1.0")</param> /// <param name="encoding">XML文档编码方式</param> /// <param name="standalone">该值必须是"yes"或"no",如果为null,Save方法不在XML声明上写出独立属性</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateXmlDocument(string rootNodeName, string version, string encoding, string standalone) { try { XmlDeclaration xmlDeclaration = this.xmlDoc.CreateXmlDeclaration(version, encoding, standalone); XmlNode root = this.xmlDoc.CreateElement(rootNodeName); this.xmlDoc.AppendChild(xmlDeclaration); this.xmlDoc.AppendChild(root); } catch { return false; } return true; } #endregion #region XmlNode #region 获取 #endregion #region 创建 /// <summary> /// 依据匹配XPath表达式的第一个节点来创建它的子节点(如果此节点已存在则追加一个新的同名节点 /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlNodeName">节点名称</param> /// <param name="innerText">节点文本值</param> /// <param name="xmlAttributeName">属性键值对</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateXmlNodeForXPath(string xpath, string xmlNodeName, string innerText, NameValueCollection xmlAttributeNameValues) { try { XmlNode xmlNode = this.xmlDoc.SelectSingleNode(xpath); if (xmlNode == null) { return false; } XmlElement subElement = this.xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText; if (this.CreateAttributeForElement(subElement, xmlAttributeNameValues)) { return false; } xmlNode.AppendChild(subElement); } catch { return false; } return true; } #endregion #region 创建或修改 /// <summary> ///依据匹配XPath表达式的第一个节点来创建或更新它的子节点(如果节点存在则更新,不存在则创建) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名</param> /// <param name="xmlNodeName">节点名称</param> /// <param name="innerText">节点文本值</param> /// <param name="xmlAttributeNameValues">属性键值对</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateOrUpdateXmlNodeForXPath(string xpath, string xmlNodeName, string innerText, NameValueCollection xmlAttributeNameValues) { bool isExistsNode = false;//标识节点是否存在 try { XmlNode xmlNode = this.xmlDoc.SelectSingleNode(xpath); if (xmlNode == null) return false; foreach (XmlNode node in xmlNode.ChildNodes) { if (node.Name.ToLower() == xmlNodeName.ToLower()) { node.InnerXml = innerText; if (CreateOrUpdateAttributeForNode(node, xmlAttributeNameValues)) return false; isExistsNode = true; break; } } if (!isExistsNode) { XmlElement subElement = this.xmlDoc.CreateElement(xmlNodeName); subElement.InnerXml = innerText; if (this.CreateAttributeForElement(subElement, xmlAttributeNameValues)) { return false; } xmlNode.AppendChild(subElement); } } catch { return false; } return true; } #endregion #region 删除 /// <summary> /// 删除XPath的XmlNode(匹配XPath结果集合) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteXmlNodeForXPath(string xpath) { try { XmlNodeList xmlNodeList = this.xmlDoc.SelectNodes(xpath); foreach (XmlNode xmlNode in xmlNodeList) { xmlNode.ParentNode.RemoveChild(xmlNode); } } catch { return false; } return true; } /// <summary> /// 删除XmlNode /// </summary> /// <param name="xmlNode">XmlNode</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteXmlNodeForNode(XmlNode xmlNode) { try { xmlNode.ParentNode.RemoveChild(xmlNode); } catch { return false; } return true; } /// <summary> /// 删除XmlElement /// </summary> /// <param name="XmlElement">XmlElement</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteXmlElementForXElement(XmlElement xmlElement) { try { xmlElement.ParentNode.RemoveChild(xmlElement); } catch { return false; } return true; } #endregion #endregion #region Attribute #region 创建 /// <summary> /// 创建XPath的Attribute(匹配XPath结果第一个) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeNameValues">XPath的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateAttributeForXPathFirst(string xpath, NameValueCollection xmlAttributeNameValues) { try { XmlNode xmlNode = this.xmlDoc.SelectSingleNode(xpath); if (xmlNode == null) return false; return CreateAttributeForNode(xmlNode, xmlAttributeNameValues); } catch { return false; } } /// <summary> /// 创建XPath的Attribute(匹配XPath结果集合) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeNameValues">XPath的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateAttributeForXPath(string xpath, NameValueCollection xmlAttributeNameValues) { try { XmlNodeList xmlNodeList = this.xmlDoc.SelectNodes(xpath); foreach (XmlNode xmlNode in xmlNodeList) { if (!CreateAttributeForNode(xmlNode, xmlAttributeNameValues)) return false; } } catch { return false; } return true; } /// <summary> /// 创建XmlNode的Attribute /// </summary> /// <param name="xmlNode">XmlNode</param> /// <param name="xmlAttributeNameValues">XmlNode的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateAttributeForNode(XmlNode xmlNode, NameValueCollection xmlAttributeNameValues) { try { if (xmlNode == null) return false; if (xmlAttributeNameValues == null) return true; foreach (string key in xmlAttributeNameValues.Keys) { if (String.IsNullOrWhiteSpace(key)) break; if (xmlNode.Attributes[key] != null) break; XmlAttribute xmlAttribute = this.xmlDoc.CreateAttribute(key); xmlAttribute.Value = xmlAttributeNameValues[key]; xmlNode.Attributes.Append(xmlAttribute); } } catch { return false; } return true; } /// <summary> /// 创建XmlElement的Attribute /// </summary> /// <param name="xmlElement">XmlElement</param> /// <param name="xmlAttributeNameValues">XmlElement的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateAttributeForElement(XmlElement xmlElement, NameValueCollection xmlAttributeNameValues) { try { if (xmlElement == null) return false; if (xmlAttributeNameValues == null) return true; foreach (string key in xmlAttributeNameValues.Keys) { if (String.IsNullOrWhiteSpace(key)) break; if (xmlElement.Attributes[key] != null) break; XmlAttribute xmlAttribute = this.xmlDoc.CreateAttribute(key); xmlAttribute.Value = xmlAttributeNameValues[key]; xmlElement.Attributes.Append(xmlAttribute); } } catch { return false; } return true; } #endregion #region 创建或修改 /// <summary> /// 创建或修改XPath的Attribute(匹配XPath结果第一个) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeNameValues">XPath的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateOrUpdateXmlAttributeForXPathFirst(string xpath, NameValueCollection xmlAttributeNameValues) { try { XmlNode xmlNode = this.xmlDoc.SelectSingleNode(xpath); if (xmlNode == null) return false; if (!CreateOrUpdateAttributeForNode(xmlNode, xmlAttributeNameValues)) return false; } catch { return false; } return true; } /// <summary> /// 创建或修改XPath的Attribute(匹配XPath结果集合) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeNameValues">XPath的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateOrUpdateXmlAttributeForXPath(string xpath, NameValueCollection xmlAttributeNameValues) { try { XmlNodeList xmlNodeList = this.xmlDoc.SelectNodes(xpath); foreach (XmlNode xmlNode in xmlNodeList) { if (!CreateOrUpdateAttributeForNode(xmlNode, xmlAttributeNameValues)) return false; } } catch { return false; } return true; } /// <summary> /// 创建或修改XmlNode的Attribute /// </summary> /// <param name="xmlNode">XmlNode</param> /// <param name="xmlAttributeNameValues">XmlNode的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateOrUpdateAttributeForNode(XmlNode xmlNode, NameValueCollection xmlAttributeNameValues) { try { if (xmlNode == null) return false; if (xmlAttributeNameValues == null) return true; foreach (string key in xmlAttributeNameValues.Keys) { if (String.IsNullOrWhiteSpace(key)) break; if (xmlNode.Attributes[key] == null) { XmlAttribute xmlAttribute = this.xmlDoc.CreateAttribute(key); xmlAttribute.Value = xmlAttributeNameValues[key]; xmlNode.Attributes.Append(xmlAttribute); } else { ((XmlAttribute)xmlNode.Attributes[key]).Value = xmlAttributeNameValues[key]; } } } catch { return false; } return true; } /// <summary> /// 创建或修改XmlElement的Attribute /// </summary> /// <param name="xmlElement">XmlElement</param> /// <param name="xmlAttributeNameValues">XmlElement的属性键值集合</param> /// <returns>成功返回true,失败返回false</returns> public bool CreateOrUpdateAttributeForElement(XmlElement xmlElement, NameValueCollection xmlAttributeNameValues) { try { if (xmlElement == null) return false; if (xmlAttributeNameValues == null) return true; foreach (string key in xmlAttributeNameValues.Keys) { if (String.IsNullOrWhiteSpace(key)) break; if (xmlElement.Attributes[key] == null) { XmlAttribute xmlAttribute = this.xmlDoc.CreateAttribute(key); xmlAttribute.Value = xmlAttributeNameValues[key]; xmlElement.Attributes.Append(xmlAttribute); } else { ((XmlAttribute)xmlElement.Attributes[key]).Value = xmlAttributeNameValues[key]; } } } catch { return false; } return true; } #endregion #region 删除 /// <summary> /// 删除XPath的Attribute(匹配XPath结果第一个) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeName">Attribute名称</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteAttributeForXPathFirst(string xpath, string xmlAttributeName) { try { XmlNodeList xmlNodeList = this.xmlDoc.SelectNodes(xpath); foreach (XmlNode xmlNode in xmlNodeList) { if (!DeleteAttributeForElement(xmlNode, xmlAttributeName)) return false; } } catch { return false; } return true; } /// <summary> /// 删除XPath的Attribute(匹配XPath结果集合) /// </summary> /// <param name="xpath">要匹配的XPath表达式(例如:"//节点名//子节点名)</param> /// <param name="xmlAttributeName">Attribute名称</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteAttributeForXPath(string xpath, string xmlAttributeName) { try { XmlNodeList xmlNodeList = this.xmlDoc.SelectNodes(xpath); foreach (XmlNode xmlNode in xmlNodeList) { if (!DeleteAttributeForElement(xmlNode, xmlAttributeName)) return false; } } catch { return false; } return true; } /// <summary> /// 删除XmlNode的指定名称Attribute /// </summary> /// <param name="xmlNode">XmlNode</param> /// <param name="xmlAttributeName">Attribute名称</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteAttributeForElement(XmlNode xmlNode, string xmlAttributeName) { try { if (xmlNode == null) return false; xmlNode.Attributes.Remove(xmlNode.Attributes[xmlAttributeName]); } catch { return false; } return true; } /// <summary> /// 删除XmlElement的指定名称Attribute /// </summary> /// <param name="xmlElement">XmlElement</param> /// <param name="xmlAttributeName">Attribute名称</param> /// <returns>成功返回true,失败返回false</returns> public bool DeleteAttributeForElement(XmlElement xmlElement, string xmlAttributeName) { try { if (xmlElement == null) return false; xmlElement.Attributes.Remove(xmlElement.Attributes[xmlAttributeName]); } catch { return false; } return true; } #endregion #endregion }