程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成的xml操作類完全實例

C#完成的xml操作類完全實例

編輯:C#入門知識

C#完成的xml操作類完全實例。本站提示廣大學習愛好者:(C#完成的xml操作類完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的xml操作類完全實例正文


本文實例講述了C#完成的xml操作類,分享給年夜家供年夜家參考,詳細以下:

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.IO;
using System.Xml;
using System.Text;
namespace MSCL
{
  /// <summary>
  /// XML 操作基類
  /// </summary>
  public class XmlHelper : IDisposable
  {
    //以下為單一功效的靜態類
    #region 讀取XML到DataSet
    /**//**************************************************
     * 函數稱號:GetXml(string XmlPath)
     * 功效解釋:讀取XML到DataSet
     * 參  數:XmlPath:xml文檔途徑
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲得xml途徑
     *     DataSet ds = EC.XmlObject.GetXml(xmlPath); //讀取xml到DataSet中
     ************************************************/
    /**//// <summary>
    /// 功效:讀取XML到DataSet中
    /// </summary>
    /// <param name="XmlPath">xml途徑</param>
    /// <returns>DataSet</returns>
    public static DataSet GetXml(string XmlPath)
    {
      DataSet ds = new DataSet();
      ds.ReadXml(@XmlPath);
      return ds;
    }
    #endregion
    #region 讀取xml文檔並前往一個節點
    /**//**************************************************
     * 函數稱號:ReadXmlReturnNode(string XmlPath,string Node)
     * 功效解釋:讀取xml文檔並前往一個節點:實用於一級節點
     * 參  數: XmlPath:xml文檔途徑;Node 前往的節點稱號
     * 順應用Xml:<?xml version="1.0" encoding="utf-8" ?>
     *      <root>
     *        <dns1>ns1.everdns.com</dns1>
     *     </root>
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲得xml途徑
     *     Response.Write(XmlObject.ReadXmlReturnNode(xmlPath, "mailmanager"));
     ************************************************/
    /**//// <summary>
    /// 讀取xml文檔並前往一個節點:實用於一級節點
    /// </summary>
    /// <param name="XmlPath">xml途徑</param>
    /// <param name="NodeName">節點</param>
    /// <returns></returns>
    public static string ReadXmlReturnNode(string XmlPath, string Node)
    {
      XmlDocument docXml = new XmlDocument();
      docXml.Load(@XmlPath);
      XmlNodeList xn = docXml.GetElementsByTagName(Node);
      return xn.Item(0).InnerText.ToString();
    }
    #endregion
    #region 查找數據,前往一個DataSet
    /**//**************************************************
     * 函數稱號:GetXmlData(string xmlPath, string XmlPathNode)
     * 功效解釋:查找數據,前往以後節點的一切上級節點,填充到一個DataSet中
     * 參  數:xmlPath:xml文檔途徑;XmlPathNode:以後節點的途徑
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     DataSet ds = new DataSet();
     *     ds = XmlObject.GetXmlData(xmlPath, "root/items");//讀取以後途徑
     *     this.GridView1.DataSource = ds;
     *     this.GridView1.DataBind();
     *     ds.Clear();
     *     ds.Dispose();
     * Xml示例:
     *     <?xml version="1.0" encoding="utf-8" ?>
     *      <root>
     *       <items name="xinnet">
     *        <url>http://www.paycenter.com.cn/cgi-bin/</url>
     *        <port>80</port>
     *       </items>
     *      </root>
     ************************************************/
    /**//// <summary>
    /// 查找數據,前往以後節點的一切上級節點,填充到一個DataSet中
    /// </summary>
    /// <param name="xmlPath">xml文檔途徑</param>
    /// <param name="XmlPathNode">節點的途徑:根節點/父節點/以後節點</param>
    /// <returns></returns>
    public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      DataSet ds = new DataSet();
      StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
      ds.ReadXml(read);
      return ds;
    }
    #endregion
    #region 更新Xml節點內容
    /**//**************************************************
     * 函數稱號:XmlNodeWordStr(string xmlPath,string Node,string Content)
     * 功效解釋:更新Xml節點內容
     * 參  數:xmlPath:xml文檔途徑;Node:以後節點的途徑;Content:內容
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     XmlObject.XmlNodeWordStr(xmlPath, "root/test", "56789"); //更新節點內容
     ************************************************/
    /**//// <summary>
    /// 更新Xml節點內容
    /// </summary>
    /// <param name="xmlPath">xml途徑</param>
    /// <param name="Node">要改換內容的節點:節點途徑 根節點/父節點/以後節點</param>
    /// <param name="Content">新的內容</param>
    public static void XmlNodeWordStr(string xmlPath, string Node, string Content)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      objXmlDoc.SelectSingleNode(Node).InnerText = Content;
      objXmlDoc.Save(xmlPath);
    }
    #endregion
    #region 刪除XML節點和此節點下的子節點
    /**//**************************************************
     * 函數稱號:XmlNodeDelete(string xmlPath,string Node)
     * 功效解釋:刪除XML節點和此節點下的子節點
     * 參  數:xmlPath:xml文檔途徑;Node:以後節點的途徑;
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     XmlObject.XmlNodeDelete(xmlPath, "root/test"); //刪除以後節點
     ************************************************/
    /**//// <summary>
    /// 刪除XML節點和此節點下的子節點
    /// </summary>
    /// <param name="xmlPath">xml文檔途徑</param>
    /// <param name="Node">節點途徑</param>
    public static void XmlNodeDelete(string xmlPath, string Node)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
      objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
      objXmlDoc.Save(xmlPath);
    }
    #endregion
    #region 拔出一個節點和此節點的子節點
    /**//**************************************************
     * 函數稱號:XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
     * 功效解釋:拔出一個節點和此節點的字節點
     * 參  數:xmlPath:xml文檔途徑;MailNode:以後節點的途徑;ChildNode:新拔出的節點;Element:拔出節點的子節點;Content:子節點的內容
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     XmlObject.XmlInsertNode(xmlPath, "root/test","test1","test2","測試內容"); //拔出一個節點和此節點的字節點
     * 生成的XML格局為
     *     <test>
     *        <test1>
     *          <test2>測試內容</test2>
     *        </test1>
     *      </test>
     ************************************************/
    /**//// <summary>
    /// 拔出一個節點和此節點的子節點
    /// </summary>
    /// <param name="xmlPath">xml途徑</param>
    /// <param name="MailNode">以後節點途徑</param>
    /// <param name="ChildNode">新拔出節點</param>
    /// <param name="Element">拔出節點的子節點</param>
    /// <param name="Content">子節點的內容,曾經運用CDATA</param>
    public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
      XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
      objRootNode.AppendChild(objChildNode);
      XmlElement objElement = objXmlDoc.CreateElement(Element);
      XmlCDataSection xcds = objXmlDoc.CreateCDataSection(Content);
      objElement.AppendChild(xcds);
      objChildNode.AppendChild(objElement);
      objXmlDoc.Save(xmlPath);
    }
    #endregion
    #region 拔出一節點,帶一屬性
    /**//**************************************************
     * 函數稱號:XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
     * 功效解釋:拔出一節點,帶一屬性
     * 參  數:xmlPath:xml文檔途徑;MailNode:以後節點的途徑;Element:新拔出的節點;Attrib:屬性稱號;AttribContent:屬性值;Content:節點的內容
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     XmlObject.XmlInsertElement(xmlPath, "root/test", "test1", "Attrib", "屬性值", "節點內容"); //拔出一節點,帶一屬性
     * 生成的XML格局為
     *     <test>
     *       <test1 Attrib="屬性值">節點內容</test1>
     *     </test>
     ************************************************/
    /**//// <summary>
    /// 拔出一節點,帶一屬性
    /// </summary>
    /// <param name="xmlPath">Xml文檔途徑</param>
    /// <param name="MainNode">以後節點途徑</param>
    /// <param name="Element">新節點</param>
    /// <param name="Attrib">屬性稱號</param>
    /// <param name="AttribContent">屬性值</param>
    /// <param name="Content">新節點值</param>
    public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
      XmlElement objElement = objXmlDoc.CreateElement(Element);
      objElement.SetAttribute(Attrib, AttribContent);
      objElement.InnerText = Content;
      objNode.AppendChild(objElement);
      objXmlDoc.Save(xmlPath);
    }
    #endregion
    #region 拔出一節點不帶屬性
    /**//**************************************************
     * 函數稱號:XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
     * 功效解釋:拔出一節點不帶屬性
     * 參  數:xmlPath:xml文檔途徑;MailNode:以後節點的途徑;Element:新拔出的節點;Content:節點的內容
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲得xml途徑
     *     XmlObject.XmlInsertElement(xmlPath, "root/test", "text1", "節點內容"); //拔出一節點不帶屬性
     * 生成的XML格局為
     *     <test>
     *         <text1>節點內容</text1>
     *     </test>
     ************************************************/
    public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
    {
      XmlDocument objXmlDoc = new XmlDocument();
      objXmlDoc.Load(xmlPath);
      XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
      XmlElement objElement = objXmlDoc.CreateElement(Element);
      objElement.InnerText = Content;
      objNode.AppendChild(objElement);
      objXmlDoc.Save(xmlPath);
    }
    #endregion
    //必需創立對象能力應用的類
    private bool _alreadyDispose = false;
    private XmlDocument xmlDoc = new XmlDocument();
    private XmlNode xmlNode;
    private XmlElement xmlElem;
    #region 結構與釋構
    public XmlHelper()
    {
    }
    ~XmlHelper()
    {
      Dispose();
    }
    protected virtual void Dispose(bool isDisposing)
    {
      if (_alreadyDispose) return;
      if (isDisposing)
      {
        //
      }
      _alreadyDispose = true;
    }
    #endregion
    #region IDisposable 成員
    public void Dispose()
    {
      Dispose(true);
      GC.SuppressFinalize(this);
    }
    #endregion
    #region 創立xml文檔
    /**//**************************************************
     * 對象稱號:XmlObject
     * 功效解釋:創立xml文檔
     * 應用示列:
     *     using EC; //援用定名空間
     *     string xmlPath = Server.MapPath("test.xml");
     *     XmlObject obj = new XmlObject();
     *     創立根節點
     *     obj.CreateXmlRoot("root");
     *     // 創立空節點
     *     //obj.CreatXmlNode("root", "Node");
     *     //創立一個帶值的節點
     *     //obj.CreatXmlNode("root", "Node", "帶值的節點");
     *     //創立一個僅帶屬性的節點
     *     //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值");
     *     //創立一個僅帶兩個屬性值的節點
     *     //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2");
     *     //創立一個帶屬性值的節點值的節點
     *     // obj.CreatXmlNode("root", "Node", "Attribe", "屬性值","節點值");
     *     //在以後節點拔出帶兩個屬性值的節點
     *     obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2","節點值");
     *     obj.XmlSave(xmlPath);
     *     obj.Dispose();
     ************************************************/
    #region 創立一個只要聲明和根節點的XML文檔
    /**//// <summary>
    /// 創立一個只要聲明和根節點的XML文檔
    /// </summary>
    /// <param name="root"></param>
    public void CreateXmlRoot(string root)
    {
      //參加XML的聲明段落
      xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
      xmlDoc.AppendChild(xmlNode);
      //參加一個根元素
      xmlElem = xmlDoc.CreateElement("", root, "");
      xmlDoc.AppendChild(xmlElem);
    }
    #endregion
    #region 在以後節點下拔出一個空節點節點
    /**//// <summary>
    /// 在以後節點下拔出一個空節點節點
    /// </summary>
    /// <param name="mainNode">以後節點途徑</param>
    /// <param name="node">節點稱號</param>
    public void CreatXmlNode(string mainNode, string node)
    {
      XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
      XmlElement objElem = xmlDoc.CreateElement(node);
      MainNode.AppendChild(objElem);
    }
    #endregion
    #region 在以後節點拔出一個僅帶值的節點
    /**//// <summary>
    /// 在以後節點拔出一個僅帶值的節點
    /// </summary>
    /// <param name="mainNode">以後節點</param>
    /// <param name="node">新節點</param>
    /// <param name="content">新節點值</param>
    public void CreatXmlNode(string mainNode, string node, string content)
    {
      XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode);
      XmlElement objElem = xmlDoc.CreateElement(node);
      objElem.InnerText = content;
      MainNode.AppendChild(objElem);
    }
    #endregion
    #region 在以後節點的拔出一個僅帶屬性值的節點
    /**//// <summary>
    /// 在以後節點的拔出一個僅帶屬性值的節點
    /// </summary>
    /// <param name="MainNode">以後節點或途徑</param>
    /// <param name="Node">新節點</param>
    /// <param name="Attrib">新節點屬性稱號</param>
    /// <param name="AttribValue">新節點屬性值</param>
    public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue)
    {
      XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
      XmlElement objElem = xmlDoc.CreateElement(Node);
      objElem.SetAttribute(Attrib, AttribValue);
      mainNode.AppendChild(objElem);
    }
    #endregion
    #region 創立一個帶屬性值的節點值的節點
    /**//// <summary>
    /// 創立一個帶屬性值的節點值的節點
    /// </summary>
    /// <param name="MainNode">以後節點或途徑</param>
    /// <param name="Node">節點稱號</param>
    /// <param name="Attrib">屬性稱號</param>
    /// <param name="AttribValue">屬性值</param>
    /// <param name="Content">節點傳情</param>
    public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Content)
    {
      XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
      XmlElement objElem = xmlDoc.CreateElement(Node);
      objElem.SetAttribute(Attrib, AttribValue);
      objElem.InnerText = Content;
      mainNode.AppendChild(objElem);
    }
    #endregion
    #region 在以後節點的拔出一個僅帶2個屬性值的節點
    /**//// <summary>
    /// 在以後節點的拔出一個僅帶2個屬性值的節點
    /// </summary>
    /// <param name="MainNode">以後節點或途徑</param>
    /// <param name="Node">節點稱號</param>
    /// <param name="Attrib">屬性稱號一</param>
    /// <param name="AttribValue">屬性值一</param>
    /// <param name="Attrib2">屬性稱號二</param>
    /// <param name="AttribValue2">屬性值二</param>
    public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2)
    {
      XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
      XmlElement objElem = xmlDoc.CreateElement(Node);
      objElem.SetAttribute(Attrib, AttribValue);
      objElem.SetAttribute(Attrib2, AttribValue2);
      mainNode.AppendChild(objElem);
    }
    #endregion
    #region 在以後節點拔出帶兩個屬性的節點
    /**//// <summary>
    /// 在以後節點拔出帶兩個屬性的節點
    /// </summary>
    /// <param name="MainNode">以後節點或途徑</param>
    /// <param name="Node">節點稱號</param>
    /// <param name="Attrib">屬性稱號一</param>
    /// <param name="AttribValue">屬性值一</param>
    /// <param name="Attrib2">屬性稱號二</param>
    /// <param name="AttribValue2">屬性值二</param>
    /// <param name="Content">節點值</param>
    public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2, string Content)
    {
      XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode);
      XmlElement objElem = xmlDoc.CreateElement(Node);
      objElem.SetAttribute(Attrib, AttribValue);
      objElem.SetAttribute(Attrib2, AttribValue2);
      objElem.InnerText = Content;
      mainNode.AppendChild(objElem);
    }
    #endregion
    #region 保留Xml
    /**//// <summary>
    /// 保留Xml
    /// </summary>
    /// <param name="path">保留確當前途徑</param>
    public void XmlSave(string path)
    {
      xmlDoc.Save(path);
    }
    #endregion
    #endregion
    #region 依據父節點屬性值讀取子節點值
    /**//**************************************************
     * 函數稱號:GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength)
     * 功效解釋:依據父節點屬性值讀取子節點值
     * 參  數: XmlPath:xml途徑;FatherElenetName:父節點名;AttributeName:屬性值;AttributeIndex:屬性索引;ArrayLength:要前往的節點數組長度
     * 順應用Xml:
     *      ArrayList al = new ArrayList();
     *      al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "/index.aspx", 0, 3);
     *      for (int i = 0; i < al.Count; i++)
     *      {
     *        Response.Write(al[i].ToString());
     *        Response.Write("<br>");
     *      }
     ************************************************/
    /**//// <summary>
    /// 依據父節點屬性讀取字節點值
    /// </summary>
    /// <param name="XmlPath">xml途徑</param>
    /// <param name="FatherElenetName">父節點名</param>
    /// <param name="AttributeName">屬性值</param>
    /// <param name="AttributeIndex">屬性索引</param>
    /// <param name="ArrayLength">要前往的節點數組長度</param>
    /// <returns></returns>
    public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength)
    {
      System.Collections.ArrayList al = new System.Collections.ArrayList();
      XmlDocument docXml = new XmlDocument();
      docXml.Load(@XmlPath);
      XmlNodeList xn = docXml.DocumentElement.ChildNodes;
      //遍歷第一層節點
      foreach (XmlElement element in xn)
      {
        //斷定父節點能否為指定節點
        if (element.Name == FatherElenetName)
        {
          //斷定父節點屬性的索引能否年夜於指定索引
          if (element.Attributes.Count < AttributeIndex)
            return null;
          //斷定父節點的屬性值能否等於指定屬性
          if (element.Attributes[AttributeIndex].Value == AttributeName)
          {
            XmlNodeList xx = element.ChildNodes;
            if (xx.Count > 0)
            {
              for (int i = 0; i < ArrayLength & i < xx.Count; i++)
              {
                al.Add(xx[i].InnerText);
              }
            }
          }
        }
      }
      return al;
    }
    #endregion
    #region 依據節點屬性讀取子節點值(較省資本形式)
    /**//**************************************************
     * 函數稱號:GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
     * 功效解釋:依據父節點屬性值讀取子節點值
     * 參  數: XmlPath:xml途徑;FatherElenetName:父節點名;AttributeName:屬性名;AttributeValue:屬性值;ArrayLength:要前往的節點數組長度
     * 順應用Xml:
     *      ArrayList al = new ArrayList();
     *      al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "@name", "/index.aspx", 3);
     *      for (int i = 0; i < al.Count; i++)
     *      {
     *        Response.Write(al[i].ToString());
     *        Response.Write("<br>");
     *      }
     ************************************************/
    /**//// <summary>
    /// 依據節點屬性讀取子節點值(較省資本形式)
    /// </summary>
    /// <param name="XmlPath">xml途徑</param>
    /// <param name="FatherElement">父節點值</param>
    /// <param name="AttributeName">屬性稱號</param>
    /// <param name="AttributeValue">屬性值</param>
    /// <param name="ArrayLength">前往的數組長度</param>
    /// <returns></returns>
    public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
    {
      System.Collections.ArrayList al = new System.Collections.ArrayList();
      XmlDocument docXml = new XmlDocument();
      docXml.Load(@XmlPath);
      XmlNodeList xn;
      xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
      XmlNodeList xx = xn.Item(0).ChildNodes;
      for (int i = 0; i < ArrayLength & i < xx.Count; i++)
      {
        al.Add(xx.Item(i).InnerText);
      }
      return al;
    }
    #endregion
  }
}

願望本文所述對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved