惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
U
Unit 42
罗磊的独立博客
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
N
Netflix TechBlog - Medium
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
博客园 - 【当耐特】
H
Help Net Security
The GitHub Blog
The GitHub Blog

博客园 - Hermes.Liu

消息队列、AMQP和RabbitMQ 继SqlPager之后推出一款可用于前台页面的分页控件--UrlPager SqlPager2.0分页控件(使用存储过程,支持.NET2.0) SqlPager最终版[附源码和示例程序](使用存储过程进行分页) SqlPager的再次改进(带数字翻页以及翻页样式设置功能) Asp.net文件的上传与下载 [转]什么是工作分解结构(WBS)? [转]什么是CMMI? 一个继承于DropDownList的树状控件 树型DataGrid的思路 [温故知新]c#的一些基础知识 IBM DB2数据库使用经验小结 提供一个基于.NET的加密/解密算法[转.CNSDN.com.cn] 关于showModalDialog 的常见问题 关于在DataTable中执行DataTable.Select("条件")返回DataTable的解决方法 在DataTable中实现DataTable.Select("Distinct")功能 常用JavaScript(二) Transact SQL备忘 Microsoft SQLHelper类概述
一个简单的XML的操纵类
Hermes.Liu · 2006-12-21 · via 博客园 - Hermes.Liu

转一位前辈的代码自己加了点注释,以备自己使用时查找,同时也希望能给需要的人一点帮助.

using System;
using System.Xml;
using System.Text;
using System.IO;
using System.Data;

namespace TestCenter.XMLData
{
    
/// <summary>
    
/// XMLHandle XML操作。
    
/// 示例:
    
/// string strXmlFile = Server.MapPath("MyXML.xml");
    
/// XMLHandle xmlHandle = new XmlControl(strXmlFile);    
    
/// DataView dv=xmlHandle.GetData("BackAdmin/User/Name");
    
/// 
    
/// </summary>

    
    
public class XMLHandle
    
{
        
protected string strXmlFile;//操纵的XML文档路径
        protected XmlDocument objXmlDoc = new XmlDocument();

        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="XmlFile">XML文件的路径 初始化类时指定</param>

        public XMLHandle(string XmlFile)
        
{
            
try
            
{
                objXmlDoc.Load(XmlFile);
            }

            
catch (System.Exception ex)
            
{
                
throw ex;
            }

            strXmlFile 
= XmlFile;
        }


        
/// <summary>
        
/// 查找指定节点的数据 返回DataView
        
/// </summary>
        
/// <param name="XmlPathNode"></param>
        
/// <returns>DataView</returns>

        public DataView GetData(string XmlPathNode)
        
{            
            DataSet dst 
= new DataSet();
            StringReader read 
= new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
            dst.ReadXml(read);
            
return dst.Tables[0].DefaultView;
        }


        
/// <summary>
        
/// 更新指定节点的内容
        
/// </summary>
        
/// <param name="XmlPathNode">节点</param>
        
/// <param name="Content">内容</param>

        public void Replace(string XmlPathNode,string Content)
        
{           
            objXmlDoc.SelectSingleNode(XmlPathNode).InnerText 
= Content;
        }


        
/// <summary>
        
/// 删除指定节点
        
/// </summary>
        
/// <param name="Node">节点</param>

        public void Delete(string Node)
        
{            
            
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
        }


        
/// <summary>
        
/// 插入一个节点和此节点的一子节点。
        
/// </summary>
        
/// <param name="MainNode">主节点</param>
        
/// <param name="ChildNode">子节点</param>
        
/// <param name="Element">元素名</param>
        
/// <param name="Content"></param>

        public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
        
{            
            XmlNode objRootNode 
= objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objChildNode 
= objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement 
= objXmlDoc.CreateElement(Element);
            objElement.InnerText 
= Content;
            objChildNode.AppendChild(objElement);
        }


        
/// <summary>
        
/// 插入一节点 包括一个属性
        
/// </summary>
        
/// <param name="MainNode"></param>
        
/// <param name="Element"></param>
        
/// <param name="Attrib"></param>
        
/// <param name="AttribContent"></param>
        
/// <param name="Content"></param>

        public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
        
{           
            XmlNode objNode 
= objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement 
= objXmlDoc.CreateElement(Element);
            objElement.SetAttribute(Attrib,AttribContent);
            objElement.InnerText 
= Content;
            objNode.AppendChild(objElement);
        }


        
/// <summary>
        
/// 插入一节点不带属性
        
/// </summary>
        
/// <param name="MainNode"></param>
        
/// <param name="Element"></param>
        
/// <param name="Content"></param>

        public void InsertElement(string MainNode,string Element,string Content)
        
{           
            XmlNode objNode 
= objXmlDoc.SelectSingleNode(MainNode);
            XmlElement objElement 
= objXmlDoc.CreateElement(Element);
            objElement.InnerText 
= Content;
            objNode.AppendChild(objElement);
        }


        
/// <summary>
        
/// 保存文档
        
/// </summary>

        public void Save()
        
{            
            
try
            
{
                objXmlDoc.Save(strXmlFile);
            }

            
catch (System.Exception ex)
            
{
                
throw new System.Exception(ex.ToString());
            }

            objXmlDoc 
= null;
        }

    }

}