完全的操作XML文件

该博客主要展示了使用C#对XML文件进行完全操作的代码。涵盖了读取、设置节点及属性值,添加、删除节点和属性,获取根元素等功能,还包含保存XML文件的方法,可帮助开发者实现对XML文件的全面管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
*Description:完全的操作XML文件
*Auther:yangtang_newton
*Email:yangtang_newton@163.com

*Dates:22004-09-10
*Copyright:USTC
*/


using System;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Web;

namespace USTC
{
    /// <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 读取指定节点的指定属性值
        /// <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) ;
           
                //获取节点的属性,并循环取出需要的属性值
                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();//删除所有子节点和属性
                    //}
                //}

            //}

        }


        #region 保存XML文件
        /// <summary>
        /// 功能:
        /// 保存XML文件
        ///
        /// </summary>
        public void SaveXmlDocument()
        {
            try
            {
                //保存设置的结果
                xmlDoc.Save( HttpContext.Current.Server.MapPath(

xmlFilePath ) );
            }
            catch(XmlException xmle)
            {
                throw xmle;
            }
        }
        #endregion

 

 

        #region 保存XML文件
        /// <summary>
        /// 功能:
        /// 保存XML文件
        ///
        /// </summary>
        public void SaveXmlDocument(string tempXMLFilePath)
        {
            try
            {
                //保存设置的结果
                xmlDoc.Save(tempXMLFilePath);
            }
            catch(XmlException xmle)
            {
                throw xmle;
            }
        }
        #endregion

 

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值