123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Xml;
- using System.Collections.Generic;
- namespace Ant.Service.Common
- {
- /// <summary>
- ///XMlHelper 的摘要说明
- /// </summary>
- public class XMlHelper
- {
- public XMlHelper()
- {
- }
- /// <summary>
- /// 读取数据
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
- /// <returns>string</returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Read(path, "/Node", "")
- * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
- ************************************************/
- public static string Read(string path, string node, string attribute)
- {
- string value = "";
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
- }
- catch { }
- return value;
- }
- ///<summary>
- /// 读取配置文件某节点的个数
- ///</summary>
- ///<param name="path">配置文件的路径</param>
- ///<param name="nodeName">要获取的节点</param>
- public static Dictionary<string, string> ReadConfig(string path, string nodeName)
- {
- Dictionary<string, string> dic = new Dictionary<string, string>();
- string absoPath = string.Empty; //绝对路径
- try
- {
- absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
- if (DirFile.IsExistFiles(absoPath))
- {
- XmlDocument xd = new XmlDocument();
- xd.Load(absoPath);
- XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相应节点的集合
- if (nodeList != null && nodeList.Count > 0)
- for (int i = 0; i < nodeList.Count; i++)
- dic.Add(nodeList.Item(i).Attributes["key"].Value, nodeList.Item(i).Attributes["value"].Value);
- }
- return dic;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
- /// <summary>
- /// 插入数据
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="node">节点</param>
- /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
- /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
- * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
- * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
- ************************************************/
- public static void Insert(string path, string node, string element, string attribute, string value)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- if (element.Equals(""))
- {
- if (!attribute.Equals(""))
- {
- XmlElement xe = (XmlElement)xn;
- xe.SetAttribute(attribute, value);
- }
- }
- else
- {
- XmlElement xe = doc.CreateElement(element);
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- xn.AppendChild(xe);
- }
- doc.Save(path);
- }
- catch
- {
- }
- }
- public static void Insertinfo(string path, string node, string element, string attribute, string value)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- if (element.Equals(""))
- {
- if (!attribute.Equals(""))
- {
- XmlElement xe = (XmlElement)xn;
- xe.SetAttribute(attribute, value);
- }
- }
- else
- {
- XmlElement xe = doc.CreateElement(element);
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- xn.AppendChild(xe);
- }
- doc.Save(path);
- }
- catch
- {
- }
- }
- static public void updateinfo(string path, string node, string attribute, string value)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node).NextSibling;
- XmlCDataSection cdata = (XmlCDataSection)xn;
- cdata.InnerText = value;
- doc.Save(path);
- }
- catch
- {
- }
- }
- /// <summary>
- /// 修改数据
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Insert(path, "/Node", "", "Value")
- * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
- ************************************************/
- public static void Update(string path, string node, string attribute, string value)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = (XmlElement)xn;
- //XmlCDataSection cdata = (XmlCDataSection)xn;
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- doc.Save(path);
- }
- catch { }
- }
- /// <summary>
- /// 删除数据
- /// </summary>
- /// <param name="path">路径</param>
- /// <param name="node">节点</param>
- /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
- /// <param name="value">值</param>
- /// <returns></returns>
- /**************************************************
- * 使用示列:
- * XmlHelper.Delete(path, "/Node", "")
- * XmlHelper.Delete(path, "/Node", "Attribute")
- ************************************************/
- public static void Delete(string path, string node, string attribute)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = (XmlElement)xn;
- if (attribute.Equals(""))
- xn.ParentNode.RemoveChild(xn);
- else
- xe.RemoveAttribute(attribute);
- doc.Save(path);
- }
- catch { }
- }
- //==================================================
- //XmlFile.xml:
- //<?xml version="1.0" encoding="utf-8"?>
- //<Root />
- //==================================================
- //使用方法:
- //string xml = Server.MapPath("XmlFile.xml");
- //插入元素
- //XmlHelper.Insert(xml, "/Root", "Studio", "", "");
- //插入元素/属性
- //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
- //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
- //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
- //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
- //插入属性
- //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
- //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
- //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
- //修改元素值
- //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
- //修改属性值
- //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
- //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
- //读取元素值
- //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
- //读取属性值
- //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
- //读取特定属性值
- //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</div>");
- //删除属性
- //XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
- //删除元素
- //XmlHelper.Delete(xml, "/Root/Studio", "");
-
- }
- }
|