using System.Xml; namespace ServiceWechat.Utility { /// /// C#XML文件操作类 包含常用XML操作信息 /// by CrazyCoder.Cn /// 转载请注明出处 /// public class XmlHelper { protected XmlDocument xdoc = new XmlDocument(); public XmlElement root; #region LoadXml /// /// 加载Xml文档 /// /// public void LoadXml(string xml) { xdoc.Load(xml); } /// /// 获取XML文件内容 /// /// public string InnerXml(string xml) { xdoc.Load(xml); return xdoc.InnerXml; } /// /// 获取XML文件内容 /// /// public void LoadFromXmlString(string xml) { xdoc.LoadXml(xml); } #endregion #region 创建一个节点,并在该节点下加入子节点 /// /// 创建一个节点,并在该节点下加入子节点 /// /// /// /// /// /// public void CreateElement(string RootNode, string ElementName, string[] ChildName, string[] ChildValue) { XmlNode Root = xdoc.SelectSingleNode(RootNode); XmlElement Node = xdoc.CreateElement(ElementName); if (ChildName.Length == ChildValue.Length) { for (int i = 0; i < ChildName.Length; i++) { XmlElement ChildNode = xdoc.CreateElement(ChildName[i]); ChildNode.InnerText = ChildValue[i]; Node.AppendChild(ChildNode); } } if (Root != null) Root.AppendChild(Node); } #endregion #region 删除某一节点 /// /// 创建一个节点,并在该节点下加入子节点 /// /// /// /// /// public void DeleteElement(string RootNode, string ChildName, string ChildValue) { XmlNode Root = xdoc.SelectSingleNode(RootNode); if (Root != null) { XmlNodeList Node = Root.ChildNodes; foreach (XmlNode xn in Node) { var selectSingleNode = xn.SelectSingleNode(ChildName); if (selectSingleNode != null && selectSingleNode.InnerText == ChildValue) { Root.RemoveChild(xn); } } } } #endregion #region 编辑某一节点 /// /// 创建一个节点,并在该节点下加入子节点 /// /// public void UpdateElement(string RootNode, string ElementName, string ElementValue, string[] ChildName, string[] ChildValue) { XmlNode Root = xdoc.SelectSingleNode(RootNode); if (Root != null) { XmlNodeList Node = Root.ChildNodes; foreach (XmlNode xn in Node) { var selectSingleNode = xn.SelectSingleNode(ElementName); if (selectSingleNode != null && selectSingleNode.InnerText == ElementValue) { if (ChildName.Length == ChildValue.Length) { for (int i = 0; i < ChildName.Length; i++) { var singleNode = xn.SelectSingleNode(ChildName[i]); if (singleNode != null) singleNode.InnerText = ChildValue[i]; } } break; } } } } public void UpdateElement(string RootNode, string[] ChildName, string[] ChildValue) { XmlNode Root = xdoc.SelectSingleNode(RootNode); if (ChildName.Length == ChildValue.Length) { for (int i = 0; i < ChildName.Length; i++) { if (Root != null) { var selectSingleNode = Root.SelectSingleNode(ChildName[i]); if (selectSingleNode != null) selectSingleNode.InnerText = ChildValue[i]; } } } } public void UpdateElement(string RootNode, string NodeName, string[] ChildName, string[] ChildValue) { XmlNode Root = xdoc.SelectSingleNode(RootNode); if (Root != null) { XmlNode Node = Root.SelectSingleNode(NodeName); if (ChildName.Length == ChildValue.Length) { for (int i = 0; i < ChildName.Length; i++) { if (Node != null) { var selectSingleNode = Node.SelectSingleNode(ChildName[i]); if (selectSingleNode != null) selectSingleNode.InnerText = ChildValue[i]; } } } } } #endregion #region 取得名称为name的结点的值 /// /// 取得名称为name的结点的值 /// /// /// public string GetValue(string name) { if (xdoc.DocumentElement != null) { XmlNode xn = FindXnByName(xdoc.DocumentElement.ChildNodes, name); if (xn == null) return null; return xn.InnerText; } return null; } #endregion #region 在某节点下取名称为name的结点的值 /// /// 在某节点下取名称为name的结点的值 /// /// public string GetValue(string NodeName, string name) { string BackString = ""; if (xdoc.DocumentElement != null) { XmlNode xn = FindXnByName(xdoc.DocumentElement.ChildNodes, NodeName); if (xn == null) { return ""; } XmlNode nn = FindXnByName(xn.ChildNodes, name); if (nn == null) { return ""; } BackString = nn.InnerText; } return BackString; } #endregion #region 通过节点名称找到指定的节点 /// /// 通过节点名称找到指定的节点 /// /// /// /// protected XmlNode FindXnByName(XmlNodeList xnl, string strName) { for (int i = 0; i < xnl.Count; i++) { var item = xnl.Item(i); if (item != null && item.LocalName == strName) { return xnl.Item(i); } } return null; } #endregion #region 为一个节点指定值 /// /// 为一个节点指定值 /// /// /// public void SetValue(string strName, string strValue) { if (xdoc.DocumentElement != null) { XmlNodeList xnl = xdoc.DocumentElement.ChildNodes; for (int i = 0; i < xnl.Count; i++) { var xmlNode = xnl.Item(i); if (xmlNode != null && xmlNode.LocalName == strName) { var item = xnl.Item(i); if (item != null) item.InnerText = strValue; return; } } } return; } #endregion #region 保存XML文档 /// /// 保存XML文档 /// public void Save(string name) { xdoc.Save(name); } #endregion } }