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

.NET操作XML完整類

編輯:關於.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
    
namespace BLL
{
    public  class XmlDoc
    {
        /// <summary>  
        /// 創建Xml文件  
        /// </summary>  
        /// <param name="xmlPath">創建文件路徑</param>  
        /// <param name="element"></param>  
        public void CreateXmlNode(string xmlPath, string element)
        {
            //實例化Xml文檔類  
            XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath, Encoding.UTF8);
            //創建Xml聲明  
            xmlTextWriter.WriteStartDocument();
            xmlTextWriter.WriteStartElement(element);
            xmlTextWriter.Flush();
            xmlTextWriter.Close();
        }
    
        /// <summary>  
        /// 新增Xml節點
        /// </summary>  
        /// <param name="xmlPath">文件路徑</param>  
        /// <param name="singleNode"></param>
        /// <param name="node"></param>  
        public void AddXmlElement(string xmlPath, string singleNode, XmlNode node)
        {
            //實例化Xml文檔類  
            XmlDocument xmlDocument = new XmlDocument();
            //加載Xml文件  
            xmlDocument.Load(xmlPath);
            xmlDocument.SelectSingleNode(singleNode).AppendChild(node);
            xmlDocument.Save(xmlPath);
        }
    
        /// <summary>  
        /// 新增Xml節點  
        /// </summary>  
        /// <param name="xmlPath"></param>  
        /// <param name="singleNode"></param>  
        /// <param name="node"></param>  
        public void AddXmlElement(string xmlPath, string singleNode, string element, string attribute, string value)
        {
            //實例化Xml文檔類  
            XmlDocument xmlDocument = new XmlDocument();
            //加載Xml文件  
            xmlDocument.Load(xmlPath);
            XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
            XmlElement xmlElement = xmlDocument.CreateElement(element); ;
            xmlElement.SetAttribute(attribute, value);
            xmlDocument.Save(xmlPath);
        }
    
    
        /// <summary>  
        /// 讀取XML文件的XmlDocument  
        /// </summary>  
        /// <param name="xmlPath">Xml文件路徑</param>  
        /// <returns></returns>  
        public XmlDocument GetXmlDocument(string xmlPath)
        {
            try
            {
                //實例化Xml文檔類  
                XmlDocument xmlDocument = new XmlDocument();
                //加載Xml文件  
                xmlDocument.Load(GetMapPath(xmlPath));
                return xmlDocument;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    
        /// <summary>  
        /// 讀取XML文件的XmlNode  
        /// </summary>  
        /// <param name="xmlPath">Xml文件路徑</param>  
        /// <param name="singleNode">選擇匹配XPath表達式的第一個XmlNode</param>  
        /// <returns></returns>  
        public XmlNode GetXmlNode(string xmlPath, string singleNode)
        {
            try
            {
                //實例化Xml文檔類  
                XmlDocument xmlDocument = new XmlDocument();
                //加載Xml文件  
                xmlDocument.Load(GetMapPath(xmlPath));
                //獲取文件內容  
                return xmlDocument.SelectSingleNode(singleNode);
            }
            catch (System.Xml.XPath.XPathException exp)
            {
                throw exp;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    
        /// <summary>  
        /// 讀取XML文件的XmlElement  
        /// </summary>  
        /// <param name="xmlPath">Xml文件路徑</param>  
        /// <param name="singleNode">選擇匹配XPath表達式的第一個XmlNode</param>  
        /// <param name="xmlNodeString">節點名稱</param>  
        /// <returns></returns>  
        public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString)
        {
            try
            {
                //實例化Xml文檔類  
                XmlDocument xmlDocument = new XmlDocument();
                //加載Xml文件  
                xmlDocument.Load(GetMapPath(xmlPath));
                //獲取文件內容  
                XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
                //提取節點名稱下的屬性值  
                return xmlNode[xmlNodeString];
            }
            catch (System.Xml.XPath.XPathException exp)
            {
                throw exp;
            }
            catch (System.Xml.XmlException exp)
            {
                throw exp;
            }
            catch (System.IO.IOException exp)
            {
                throw exp;
            }
            catch (ArgumentNullException exp)
            {
                throw exp;
            }
            catch (ArgumentException exp)
            {
                throw exp;
            }
            catch (Exception exp)
            {
                throw exp;
            }
    
        }
    
        /// <summary>  
        /// 讀取XML文件的節點屬性  
        /// </summary>  
        /// <param name="xmlPath">Xml文件路徑</param>  
        /// <param name="singleNode">選擇匹配XPath表達式的第一個XmlNode</param>  
		///查看本欄目
		
							
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved