XmlHelper

本文提供了一个全面的XML操作类库示例,包括节点增删改查等核心功能,并详细介绍了如何使用C#进行XML文件的读写及节点操作。
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,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值