Frankwangyifang

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
/// <summary>
/// Summary description for XMLReSovle
/// </summary>
public class XMLReSovle
{
    public XMLReSovle()
    {
    }
    private XmlDocument xmlDoc = new XmlDocument();
    private string xmlFileName = null;
    public string FilePath
    {
        set { xmlFileName = value; }
    }
    //创建XML操作对象
    public void OpenXML()
    {
        xmlDoc.Load(xmlFileName);
    }
    #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>
    /// <param name="tempXmlNode">给定结点</param>
    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(xmlFileName);
        }
        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
    #region 创建一个XML 文件
    public bool CreateXMLFile(string xPath)
    {
        FileStream file = new FileStream("~/aaa.xml", FileMode.Create);
    }
    #endregion
}
posted on 2009-11-10 12:25  Frankwangyifang  阅读(333)  评论(0编辑  收藏  举报