XMLHelper

XML研究 专栏收录该内容
6 篇文章 0 订阅
全部源码下载地址: http://download.csdn.net/source/855790
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections;
  11. //引用命名空间
  12. using System.Xml;
  13. namespace xmlOp
  14. {
  15. /// <summary>
  16. /// XmlOp类提供对XML数据库的读写
  17. /// </summary>
  18.     public class XmlOp
  19.     {
  20.         //声明一个XmlDocument空对象
  21.         protected XmlDocument XmlDoc = new XmlDocument();
  22.         /// <summary>
  23.         /// 构造函数,导入Xml文件
  24.         /// </summary>
  25.         /// <param name="xmlFile">文件虚拟路径</param>
  26.         public XmlOp(string xmlFile)
  27.         {
  28.             try
  29.             {
  30.                 XmlDoc.Load(GetXmlFilePath(xmlFile));   //载入Xml文档
  31.             }
  32.             catch (System.Exception ex)
  33.             {
  34.                 throw ex;
  35.             }
  36.         }
  37.         /// <summary>
  38.         /// 析构函数
  39.         /// </summary>
  40.         ~XmlOp()
  41.         {
  42.             XmlDoc = null;  //释放XmlDocument对象
  43.         }
  44.         /// <summary>
  45.         /// 保存文件
  46.         /// </summary>
  47.         /// <param name="filePath">文件虚拟路径</param>
  48.         public void Save(string filePath)
  49.         {
  50.             try
  51.             {
  52.                 XmlDoc.Save(GetXmlFilePath(filePath));
  53.             }
  54.             catch (System.Exception ex)
  55.             {
  56.                 throw ex;
  57.             }
  58.         }
  59.         /// <summary>
  60.         /// 返回Xml文件实际路径
  61.         /// </summary>
  62.         /// <param name="xmlFile">文件虚拟路径</param>
  63.         /// <returns></returns>
  64.         public string GetXmlFilePath(string xmlFile)
  65.         {
  66.             return HttpContext.Current.Server.MapPath(xmlFile);
  67.         }
  68.         /// <summary>
  69.         /// 根据Xml文件的节点路径,返回一个DataSet数据集
  70.         /// </summary>
  71.         /// <param name="XmlPathNode">Xml文件的某个节点</param>
  72.         /// <returns></returns>
  73.         public DataSet GetDs(string XmlPathNode)
  74.         {
  75.             DataSet ds = new DataSet();
  76.             try
  77.             {                
  78.                 System.IO.StringReader read = new System.IO.StringReader(XmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
  79.                 ds.ReadXml(read);   //利用DataSet的ReadXml方法读取StringReader文件流
  80.                 read.Close();
  81.             }
  82.             catch
  83.             { }
  84.             return ds;
  85.         }
  86.         /// <summary>
  87.         /// 属性查询,返回属性值
  88.         /// </summary>
  89.         /// <param name="XmlPathNode">属性所在的节点</param>
  90.         /// <param name="Attrib">属性</param>
  91.         /// <returns></returns>
  92.         public string SelectAttrib(string XmlPathNode, string Attrib)
  93.         {
  94.             string _strNode="";
  95.             try
  96.             {
  97.                 _strNode = XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value;
  98.             }
  99.             catch
  100.             { }
  101.             return _strNode;
  102.         }
  103.         /// <summary>
  104.         /// 节点查询,返回节点值
  105.         /// </summary>
  106.         /// <param name="XmlPathNode">节点的路径</param>
  107.         /// <returns></returns>
  108.         public string SelectNodeText(string XmlPathNode)
  109.         {
  110.             string _nodeTxt = XmlDoc.SelectSingleNode(XmlPathNode).InnerText;
  111.             if (_nodeTxt == null || _nodeTxt == "")
  112.                 return "";
  113.             else
  114.                 return _nodeTxt;
  115.         }
  116.         /// <summary>
  117.         /// 节点值查询判断
  118.         /// </summary>
  119.         /// <param name="XmlPathNode">父节点</param>
  120.         /// <param name="index">节点索引</param>
  121.         /// <param name="NodeText">节点值</param>
  122.         /// <returns></returns>
  123.         public bool SelectNode(string XmlPathNode, int index, string NodeText)
  124.         {
  125.             try
  126.             {
  127.                 XmlNodeList _NodeList = XmlDoc.SelectNodes(XmlPathNode);
  128.                 //循环遍历节点,查询是否存在该节点
  129.                 for (int i = 0; i < _NodeList.Count; i++)
  130.                 {
  131.                     if (_NodeList[i].ChildNodes[index].InnerText == NodeText)
  132.                     {
  133.                         return true;
  134.                         break;
  135.                     }
  136.                 }
  137.             }
  138.             catch
  139.             {                
  140.             }
  141.             return false;
  142.         }
  143.         /// <summary>
  144.         /// 获取子节点个数
  145.         /// </summary>
  146.         /// <param name="XmlPathNode">父节点</param>
  147.         /// <returns></returns>
  148.         public int NodeCount(string XmlPathNode)
  149.         {
  150.             int i = 0;
  151.             try
  152.             {
  153.                 i=XmlDoc.SelectSingleNode(XmlPathNode).ChildNodes.Count;                
  154.             }
  155.             catch
  156.             {
  157.                 i=0;
  158.             }
  159.             return i;
  160.         }
  161.         /// <summary>
  162.         /// 更新一个节点的内容
  163.         /// </summary>
  164.         /// <param name="XmlPathNode">节点的路径</param>
  165.         /// <param name="Content">新的节点值</param>
  166.         /// <returns></returns>
  167.         public bool UpdateNode(string XmlPathNode,string NodeContent)
  168.         {
  169.             try
  170.             {
  171.                 XmlDoc.SelectSingleNode(XmlPathNode).InnerText = NodeContent;
  172.                 return true;
  173.             }
  174.             catch
  175.             {
  176.                 return false;
  177.             }
  178.         }
  179.         /// <summary>
  180.         /// 更新N个节点值
  181.         /// </summary>
  182.         /// <param name="XmlParentNode">父节点</param>
  183.         /// <param name="XmlNode">子节点</param>
  184.         /// <param name="NodeContent">子节点内容</param>
  185.         /// <returns></returns>
  186.         public bool UpdateNode(string XmlParentNode, string[] XmlNode, string[] NodeContent)
  187.         {               
  188.             try
  189.             {
  190.                 //根据节点数组循环修改节点值
  191.                 for (int i = 0; i < XmlNode.Length; i++)
  192.                 {
  193.                     XmlDoc.SelectSingleNode(XmlParentNode+"/"+XmlNode[i]).InnerText = NodeContent[i];
  194.                 }
  195.                 return true;
  196.             }
  197.             catch
  198.             {
  199.                 return false;
  200.             }
  201.         }
  202.         /// <summary>
  203.         /// 修改属性
  204.         /// </summary>
  205.         /// <param name="XmlPathNode">属性所在的节点</param>
  206.         /// <param name="Attrib">属性名</param>
  207.         /// <param name="Content">属性值</param>
  208.         /// <returns></returns>
  209.         public bool UpdateAttrib(string XmlPathNode, string Attrib,string AttribContent)
  210.         {
  211.             try
  212.             {
  213.                 //修改属性值
  214.                 XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value = AttribContent;
  215.                 return true;
  216.             }
  217.             catch
  218.             {
  219.                 return false;
  220.             }
  221.         }
  222.         /// <summary>
  223.         /// 添加属性
  224.         /// </summary>
  225.         /// <param name="MainNode">属性所在节点</param>
  226.         /// <param name="Attrib">属性名</param>
  227.         /// <param name="AttribContent">属性值</param>
  228.         /// <returns></returns>
  229.         public bool InsertAttrib(string MainNode,string Attrib, string AttribContent)
  230.         {
  231.             try
  232.             {
  233.                 XmlElement objNode = (XmlElement)XmlDoc.SelectSingleNode(MainNode); //强制转化成XmlElement对象
  234.                 objNode.SetAttribute(Attrib, AttribContent);    //XmlElement对象添加属性方法    
  235.                 return true;
  236.             }
  237.             catch
  238.             {
  239.                 return false;
  240.             }
  241.         }
  242.         /// <summary>
  243.         /// 插入一个节点,带N个子节点
  244.         /// </summary>
  245.         /// <param name="MainNode">插入节点的父节点</param>
  246.         /// <param name="ChildNode">插入节点的元素名</param>
  247.         /// <param name="Element">插入节点的子节点名数组</param>
  248.         /// <param name="Content">插入节点的子节点内容数组</param>
  249.         /// <returns></returns>
  250.         public bool InsertNode(string MainNode, string ChildNode, string[] Element, string[] Content)
  251.         {
  252.             try
  253.             {
  254.                 XmlNode objRootNode = XmlDoc.SelectSingleNode(MainNode);    //声明XmlNode对象
  255.                 XmlElement objChildNode = XmlDoc.CreateElement(ChildNode);  //创建XmlElement对象
  256.                 objRootNode.AppendChild(objChildNode);  
  257.                 for (int i = 0; i < Element.Length; i++)    //循环插入节点元素
  258.                 {
  259.                     XmlElement objElement = XmlDoc.CreateElement(Element[i]);
  260.                     objElement.InnerText = Content[i];
  261.                     objChildNode.AppendChild(objElement);
  262.                 }
  263.                 return true;
  264.             }
  265.             catch
  266.             {
  267.                 return false;
  268.             }
  269.         }
  270.         /// <summary>
  271.         /// 删除一个节点
  272.         /// </summary>
  273.         /// <param name="Node">要删除的节点</param>
  274.         /// <returns></returns>
  275.         public bool DeleteNode(string Node)
  276.         {
  277.             try
  278.             {
  279.                 //XmlNode的RemoveChild方法来删除节点及其所有子节点
  280.                 XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
  281.                 return true;
  282.             }
  283.             catch
  284.             {
  285.                 return false;
  286.             }
  287.         }
  288.     }
  289. }
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值