XmlHelper

简介:
1
2
3
4
5
6
using  System;
     using  System.Collections.Generic;
     using  System.Data;
     using  System.IO;
     using  System.Xml;
     using  System.Xml.XPath;

 

1
2
3
4
5
6
7
8
public  class  XmlHelper
    {
        private  XPathNavigator nav;
        private  XmlNode node;
        private  XPathNodeIterator NodeIter;
        protected  XmlDocument objXmlDoc = new  XmlDocument();
        private  string  strExpression;
        protected  string  strXmlFile;

 

 

1
2
3
4
5
6
7
8
9
10
11
12
public  XmlHelper( string  XmlFile)
  {
      try
        {
           this .objXmlDoc.Load(XmlFile);
          }
          catch  (Exception ex)
          {
              throw  ex;
          }
          this .strXmlFile = XmlFile;
      }

 

 

1
2
3
4
5
6
7
8
9
10
public  void  Delete( string  Node)
     {
         string  mainNode = Node.Substring(0, Node.LastIndexOf( "/" ));
         this .objXmlDoc.SelectSingleNode(mainNode).RemoveChild( this .objXmlDoc.SelectSingleNode(Node));
     }
 
     public  void  DeleteChildren( string  Node)
     {
         this .objXmlDoc.SelectSingleNode(Node).RemoveAll();
     }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public  DataView GetData( string  XmlPathNode)
      {
          DataSet ds = new  DataSet();
          this .nav = this .objXmlDoc.CreateNavigator();
          this .nav = this .nav.SelectSingleNode(XmlPathNode);
          if  ( this .nav == null )
          {
              throw  new  Exception( "没有找到结点" );
          }
          StringReader read = new  StringReader( this .nav.get_OuterXml());
          ds.ReadXml(read);
          if  (ds.Tables.Count > 0)
          {
              return  ds.Tables[0].DefaultView;
          }
          return  null ;
      }

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  string  GetElementValue( string  MainNode)
       {
           this .nav = this .objXmlDoc.CreateNavigator();
           this .strExpression = string .Format( "{0}" , MainNode);
           this .nav = this .nav.SelectSingleNode( this .strExpression);
           return  this .nav.get_Value();
       }
 
       public  string  GetElementValue( string  MainNode, string  Element)
       {
           this .nav = this .objXmlDoc.CreateNavigator();
           this .strExpression = string .Format( "{0}/{1}" , MainNode, Element);
           this .nav = this .nav.SelectSingleNode( this .strExpression);
           return  this .nav.get_Value();
       }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public  string  GetNodeAttrValue( string  MainNode, string  Attr)
       {
           this .nav = this .objXmlDoc.CreateNavigator();
           this .strExpression = string .Format( "{0}@{1}" , MainNode, Attr);
           this .nav.SelectSingleNode( this .strExpression);
           return  this .nav.get_Value();
       }
 
       public  List< string > GetSubNodes( string  MainNode)
       {
           List< string > lst = new  List< string >();
           this .nav = this .objXmlDoc.CreateNavigator();
           this .nav = this .nav.SelectSingleNode(MainNode);
           this .NodeIter = this .nav.SelectChildren(XPathNodeType.Element);
           foreach  (XPathNavigator n in  this .NodeIter)
           {
               lst.Add(n.get_Value());
           }
           return  lst;
       }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public  void  InsertElement( string  MainNode, string  Element, string  Content)
       {
           XmlNode objNode = this .objXmlDoc.SelectSingleNode(MainNode);
           XmlElement objElement = this .objXmlDoc.CreateElement(Element);
           objElement.InnerText = Content;
           objNode.AppendChild(objElement);
       }
 
       public  void  InsertElement( string  MainNode, string  Element, string  Attrib, string  AttribContent, string  Content)
       {
           XmlNode objNode = this .objXmlDoc.SelectSingleNode(MainNode);
           XmlElement objElement = this .objXmlDoc.CreateElement(Element);
           objElement.SetAttribute(Attrib, AttribContent);
           objElement.InnerText = Content;
           objNode.AppendChild(objElement);
       }
 
       public  void  InsertNode( string  MainNode, string  ChildNode, string  Element, string  Content)
       {
           XmlNode objRootNode = this .objXmlDoc.SelectSingleNode(MainNode);
           XmlElement objChildNode = this .objXmlDoc.CreateElement(ChildNode);
           objRootNode.AppendChild(objChildNode);
           XmlElement objElement = this .objXmlDoc.CreateElement(Element);
           objElement.InnerText = Content;
           objChildNode.AppendChild(objElement);
       }

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  bool  IsExists( string  Node)
       {
           try
           {
               if  ( this .objXmlDoc.SelectSingleNode(Node) == null )
               {
                   return  false ;
               }
               return  true ;
           }
           catch  (Exception)
           {
               return  false ;
           }
       }

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    public  void  RemoveAll( string  Node)
         {
             XmlNodeList xnl = this .objXmlDoc.SelectNodes(Node);
             string  mainNode = Node.Substring(0, Node.LastIndexOf( "/" ));
             foreach  (XmlNode xn in  xnl)
             {
                 xn.RemoveAll();
             }
         }
 
         public  void  Replace( string  XmlPathNode, string  Content)
         {
             this .objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
         }
 
         public  void  Save()
         {
             try
             {
                 this .objXmlDoc.Save( this .strXmlFile);
             }
             catch  (Exception ex)
             {
                 throw  ex;
             }
             this .objXmlDoc = null ;
         }
     }
}


    本文转自曾祥展博客园博客,原文链接:http://www.cnblogs.com/zengxiangzhan/archive/2010/01/19/1651836.html ,如需转载请自行联系原作者

相关文章
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
303 116
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
492 45
Meta SAM3开源:让图像分割,听懂你的话
|
14天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
694 222
|
2天前
|
Windows
dll错误修复 ,可指定下载dll,regsvr32等
dll错误修复 ,可指定下载dll,regsvr32等
135 95
|
12天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1699 158
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
951 62