| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using System.Xml;
- namespace ServiceWechat.Utility
- {
- /// <summary>
- /// C#XML文件操作类 包含常用XML操作信息
- /// by CrazyCoder.Cn
- /// 转载请注明出处
- /// </summary>
- public class XmlHelper
- {
- protected XmlDocument xdoc = new XmlDocument();
- public XmlElement root;
- #region LoadXml
- /// <summary>
- /// 加载Xml文档
- /// </summary>
- /// <param name="xml"></param>
- public void LoadXml(string xml)
- {
- xdoc.Load(xml);
- }
- /// <summary>
- /// 获取XML文件内容
- /// </summary>
- /// <param name="xml"></param>
- public string InnerXml(string xml)
- {
- xdoc.Load(xml);
- return xdoc.InnerXml;
- }
- /// <summary>
- /// 获取XML文件内容
- /// </summary>
- /// <param name="xml"></param>
- public void LoadFromXmlString(string xml)
- {
- xdoc.LoadXml(xml);
- }
- #endregion
- #region 创建一个节点,并在该节点下加入子节点
- /// <summary>
- /// 创建一个节点,并在该节点下加入子节点
- /// </summary>
- /// <param name="RootNode"></param>
- /// <param name="ElementName"></param>
- /// <param name="ChildName"></param>
- /// <param name="ChildValue"></param>
- /// <returns></returns>
- 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 删除某一节点
- /// <summary>
- /// 创建一个节点,并在该节点下加入子节点
- /// </summary>
- /// <param name="RootNode"></param>
- /// <param name="ChildName"></param>
- /// <param name="ChildValue"></param>
- /// <returns></returns>
- 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 编辑某一节点
- /// <summary>
- /// 创建一个节点,并在该节点下加入子节点
- /// </summary>
- /// <returns></returns>
- 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的结点的值
- /// <summary>
- /// 取得名称为name的结点的值
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- 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的结点的值
- /// <summary>
- /// 在某节点下取名称为name的结点的值
- /// </summary>
- /// <returns></returns>
- 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 通过节点名称找到指定的节点
- /// <summary>
- /// 通过节点名称找到指定的节点
- /// </summary>
- /// <param name="xnl"></param>
- /// <param name="strName"></param>
- /// <returns></returns>
- 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 为一个节点指定值
- /// <summary>
- /// 为一个节点指定值
- /// </summary>
- /// <param name="strName"></param>
- /// <param name="strValue"></param>
- 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文档
- /// <summary>
- /// 保存XML文档
- /// </summary>
- public void Save(string name)
- {
- xdoc.Save(name);
- }
- #endregion
- }
- }
|