DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> C#.net對XML文件的操作,福cs源代碼
C#.net對XML文件的操作,福cs源代碼
編輯:XML詳解     
#.net對XML文件類封裝:這個東西是很早的時候寫的,是基於COM模型的,所以大家還可以優化。下面我把這樣實現對XML文件操作的類的C#.Net代碼貼出,如大家要轉載,請保留本人的版權。

/*
*Description:完全的操作XML文件
*Auther:mingziweb_天很藍
*Email:chongchong2008@msn.com
*Dates:22004-09-10
*Copyright:ChongChong2008 YiChang HuBei China
*/


using System;
using System.XML;
using System.XML.Xsl;
using System.XML.XPath;

using System.Web;

namespace Library.ClassLibrary.XML
{
    /// <summary>
    /// XML 的摘要說明。
    /// </summary>
    public class XML
    {
        public enum enumXMLPathType
        {
            AbsolutePath,
            VirtualPath
        }

        private string XMLFilePath ;
        private enumXmlPathType XMLFilePathType ;
        private XmlDocument xmlDoc = new XMLDocument() ;

        public string XMLFilePath
        {
            set
            {
                XMLFilePath = value ;

            }
        }

        public enumXmlPathType XMLFilePathTyp
        {
            set
            {
                XMLFilePathType = value ;
            }
        }

        public XML( string tempXMLFilePath )
        {
            //
            // TODO: 在此處添加構造函數邏輯
            //

            this.xmlFilePathType = enumXMLPathType.VirtualPath ;
            this.xmlFilePath = tempXMLFilePath ;
            GetXMLDocument() ;
            //xmlDoc.Load( XMLFilePath ) ;
        }

        public XML( string tempXmlFilePath , enumXmlPathType tempXMLFilePathType )
        {
            //
            // TODO: 在此處添加構造函數邏輯
            //
            this.xmlFilePathType = tempXMLFilePathType ;
            this.xmlFilePath = tempXMLFilePath ;
            GetXMLDocument() ;
        }

        /// </summary>
        /// <param name="strEntityTypeName">實體類的名稱</param>
        /// <returns>指定的XML描述文件的路徑</returns>
        private XmlDocument GetXMLDocument()
        {
            XMLDocument doc=null;

            if( this.xmlFilePathType == enumXMLPathType.AbsolutePath )
            {
                doc = GetXmlDocumentFromFile( XMLFilePath ) ;
            }
            else if( this.xmlFilePathType == enumXMLPathType.VirtualPath )
            {
    ;            doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(XMLFilePath)) ;
            }
            return doc;
        }

        private XmlDocument GetXmlDocumentFromFile(string tempXMLFilePath)
        {
            string xmlFileFullPath = tempXMLFilePath ;

                    xmlDoc.Load(XMLFileFullPath) ;
            return XMLDoc ;
        }

        #region 讀取指定節點的指定屬性值值
                XmlAttributeCollection xmlAttr = XMLNode.Attributes ;
                for(int i=0 ; i<XMLAttr.Count ; i++)
                {
                    if ( xmlAttr.Item(i).Name == XMLNodeAttribute )
                    {
                        xmlAttr.Item(i).Value = XMLNodeAttributeValue;
                        break ;
                    }
                }
            }
            catch(XmlException XMLe)
            {
                throw XMLe ;
            }
        }
        #endregion

        /// <summary>
        /// 獲取XML文件的根元素
        /// </summary>
        public XmlNode GetXMLRoot()
        {
            return XMLDoc.DocumentElement ;
        }

        /// <summary>
        /// 在根節點下添加父節點
        /// </summary>
        public void AddParentNode(string parentNode)
        {
            XmlNode root = GetXMLRoot() ;
            XmlNode parentXmlNode = XMLDoc.CreateElement(parentNode) ;

            root.AppendChild(parentXMLNode) ;
        }

        /// <summary>
        /// 向一個已經存在的父節點中插入一個子節點
        /// </summary>
        public void AddChildNode( string parentNodePath,string childNodePath )
        {
            XmlNode parentXmlNode = XMLDoc.SelectSingleNode(parentNodePath) ;
            XmlNode childXmlNode = XMLDoc.CreateElement(childNodePath) ;

            parentXmlNode.AppendChild( childXMLNode ) ;
        }

        /// <summary>
        /// 向一個節點添加屬性
        /// </summary>
        public void AddAttribute( string NodePath , string NodeAttribute)
        {
            XmlAttribute nodeAttribute = XMLDoc.CreateAttribute(NodeAttribute) ;
            XmlNode nodePath = XMLDoc.SelectSingleNode( NodePath ) ;
            nodePath.Attributes.Append(nodeAttribute) ;
        }

        /// <summary>
        /// 刪除一個節點的屬性
        /// </summary>
        public void DeleteAttribute( string NodePath , string NodeAttribute , string NodeAttributeValue)
        {
            XMLNodeList nodePath =

XMLDoc.SelectSingleNode( NodePath ).ChildNodes;

            foreach(XMLNode xn in nodePath)
            {
                XmlElement xe=(XMLElement)xn;

                if(xe.GetAttribute(NodeAttribute)==NodeAttributeValue)
                {
                    xe.RemoveAttribute(NodeAttribute);//刪除屬性
                }
            }

        }

        /// <summary>
        /// 刪除一個節點
        /// </summary>
        public void DeleteXmlNode(string tempXMLNode)
        {
            //XmlNodeList xmlNodePath=xmlDoc.SelectSingleNode(tempXMLNode).ChildNodes;

            XmlNode xmlNodePath = xmlDoc.SelectSingleNode( tempXMLNode ) ;
            xmlNodePath.ParentNode.RemoveChild( XMLNodePath ) ;
            //foreach(XmlNode xn in XMLNodePath)
            //{
                //XmlElement xe=(XMLElement)xn;
                //xe.RemoveAll();
                //xe.RemoveChild(xn);

                //xn.RemoveAll();

                //if(xe.HasChildNodes)
                //{

                    //foreach(XMLNode xn in xe)
                    //{

                        //xn.RemoveAll();//刪除所有子節點和屬性
                    /


        /// <summary>
        /// 功能:
        /// 讀取指定節點的指定屬性
        ///
        /// 參數:
        /// 參數一:節點名稱
        /// 參數二:此節點的屬性
        /// </summary>
        /// <param name="strNode"></param>
        /// <param name="strAttribute"></param>
        /// <returns></returns>
        public string GetXMLNodeValue(string strNode,string strAttribute)
        {
            string strReturn = "";
            try
            {
                //根據指定路徑獲取節點
                XmlNode xmlNode = XMLDoc.SelectSingleNode(strNode) ;           
                //獲取節點的屬性,並循環取出需要的屬性
                XmlAttributeCollection xmlAttr = XMLNode.Attributes ;

                for(int i=0 ;i<XMLAttr.Count; i++)
                {
                    if (XMLAttr.Item(i).Name == strAttribute)
                        strReturn = XMLAttr.Item(i).Value ;
                }
            }
            catch(XmlException XMLe)
            {
                throw XMLe ;
            }
            return strReturn ;
        }
        #endregion

 


        #region
        /// <summary>
        /// 功能:
        /// 讀取指定節點的
        ///
        /// 參數:
        /// 參數:節點名稱
        /// </summary>
        /// <param name="strNode"></param>
        /// <returns></returns>
        public string GetXMLNodeValue(string strNode)
        {
            string strReturn = String.Empty ;
            try
;       {
                //根據路徑獲取節點
                XmlNode xmlNode = XMLDoc.SelectSingleNode(strNode) ;
                strReturn = XMLNode.InnerText ;
            }
            catch(XmlException XMLe)
            {
                System.Console.WriteLine(XMLe.Message) ;
            }
            return strReturn ;
        }
        #endregion

        #region 設置節點
        /// <summary>
        /// 功能:
        /// 設置節點
        ///
        /// 參數:
        ///    參數一:節點的名稱
        ///    參數二:節點
        ///   
        /// </summary>
        /// <param name="strNode"></param>
        /// <param name="newValue"></param>
        public void SetXmlNodeValue(string xmlNodePath,string XMLNodeValue)
        {
            try
            {
                //根據指定路徑獲取節點
                XmlNode xmlNode = xmlDoc.SelectSingleNode(XMLNodePath) ;           
                //設置節點
                xmlNode.InnerText = XMLNodeValue ;
            }
            catch(XmlException XMLe)
            {
                throw XMLe ;
            }
        }
        #endregion

        #region 設置節點的屬性
        /// <summary>
        /// 功能:
        /// 設置節點的屬性
        ///
        /// 參數:
        /// 參數一:節點名稱
        /// 參數二:屬性名稱
        /// 參數三:屬性
        ///
        /// </summary>
        /// <param name="XMLNodePath"></param>
        /// <param name="XMLNodeAttribute"></param>
        /// <param name="XMLNodeAttributeValue"></param>
        public void SetXmlNodeValue(string xmlNodePath,string xmlNodeAttribute,string XMLNodeAttributeValue)
        {
            try
            {
                //根據指定路徑獲取節點
                XmlNode xmlNode = xmlDoc.SelectSingleNode(XMLNodePath) ;
           
                //獲取節點的屬性,並循環取出需要的屬性

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved