码迷,mamicode.com
首页 > Windows程序 > 详细

C#:XML操作类

时间:2015-08-10 19:54:14      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Web;
  5 using System.Web.Security;
  6 using System.Web.UI;
  7 using System.Web.UI.WebControls;
  8 using System.Web.UI.WebControls.WebParts;
  9 using System.Web.UI.HtmlControls;
 10 using System.Xml;
 11 
 12 namespace PuTianCheng
 13 {
 14     /// <summary>
 15     /// XmlHelper 的摘要说明
 16     /// </summary>
 17     public class XmlHelper
 18     {
 19         public XmlHelper()
 20         {
 21         }
 22 
 23         /// <summary>
 24         /// 读取数据
 25         /// </summary>
 26         /// <param name="path">路径</param>
 27         /// <param name="node">节点</param>
 28         /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
 29         /// <returns>string</returns>
 30         /**************************************************
 31          * 使用示列:
 32          * XmlHelper.Read(path, "/Node", "")
 33          * XmlHelper.Read(path, "/Node/Element[@Attribute=‘Name‘]", "Attribute")
 34          ************************************************/
 35         public static string Read(string path, string node, string attribute)
 36         {
 37             string value = "";
 38             try
 39             {
 40                 XmlDocument doc = new XmlDocument();
 41                 doc.Load(path);
 42                 XmlNode xn = doc.SelectSingleNode(node);
 43                 value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
 44             }
 45             catch { }
 46             return value;
 47         }
 48 
 49         /// <summary>
 50         /// 插入数据
 51         /// </summary>
 52         /// <param name="path">路径</param>
 53         /// <param name="node">节点</param>
 54         /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
 55         /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
 56         /// <param name="value"></param>
 57         /// <returns></returns>
 58         /**************************************************
 59          * 使用示列:
 60          * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
 61          * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
 62          * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
 63          ************************************************/
 64         public static void Insert(string path, string node, string element, string attribute, string value)
 65         {
 66             try
 67             {
 68                 XmlDocument doc = new XmlDocument();
 69                 doc.Load(path);
 70                 XmlNode xn = doc.SelectSingleNode(node);
 71                 if (element.Equals(""))
 72                 {
 73                     if (!attribute.Equals(""))
 74                     {
 75                         XmlElement xe = (XmlElement)xn;
 76                         xe.SetAttribute(attribute, value);
 77                     }
 78                 }
 79                 else
 80                 {
 81                     XmlElement xe = doc.CreateElement(element);
 82                     if (attribute.Equals(""))
 83                         xe.InnerText = value;
 84                     else
 85                         xe.SetAttribute(attribute, value);
 86                     xn.AppendChild(xe);
 87                 }
 88                 doc.Save(path);
 89             }
 90             catch { }
 91         }
 92 
 93         /// <summary>
 94         /// 修改数据
 95         /// </summary>
 96         /// <param name="path">路径</param>
 97         /// <param name="node">节点</param>
 98         /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
 99         /// <param name="value"></param>
100         /// <returns></returns>
101         /**************************************************
102          * 使用示列:
103          * XmlHelper.Insert(path, "/Node", "", "Value")
104          * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
105          ************************************************/
106         public static void Update(string path, string node, string attribute, string value)
107         {
108             try
109             {
110                 XmlDocument doc = new XmlDocument();
111                 doc.Load(path);
112                 XmlNode xn = doc.SelectSingleNode(node);
113                 XmlElement xe = (XmlElement)xn;
114                 if (attribute.Equals(""))
115                     xe.InnerText = value;
116                 else
117                     xe.SetAttribute(attribute, value);
118                 doc.Save(path);
119             }
120             catch { }
121         }
122 
123         /// <summary>
124         /// 删除数据
125         /// </summary>
126         /// <param name="path">路径</param>
127         /// <param name="node">节点</param>
128         /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
129         /// <param name="value"></param>
130         /// <returns></returns>
131         /**************************************************
132          * 使用示列:
133          * XmlHelper.Delete(path, "/Node", "")
134          * XmlHelper.Delete(path, "/Node", "Attribute")
135          ************************************************/
136         public static void Delete(string path, string node, string attribute)
137         {
138             try
139             {
140                 XmlDocument doc = new XmlDocument();
141                 doc.Load(path);
142                 XmlNode xn = doc.SelectSingleNode(node);
143                 XmlElement xe = (XmlElement)xn;
144                 if (attribute.Equals(""))
145                     xn.ParentNode.RemoveChild(xn);
146                 else
147                     xe.RemoveAttribute(attribute);
148                 doc.Save(path);
149             }
150             catch { }
151         }
152 
153         /// <summary>
154         /// 获取数据集合
155         /// </summary>
156         /// <param name="path">路径</param>
157         /// <param name="node">节点</param>
158         /// <param name="value"></param>
159         /// <returns>Dictionary集合</returns>
160      public static Dictionary<int, List<string>> ReadingXml(string path,string node)
161          {
162              XmlDocument xmlDoc = new XmlDocument();
163              xmlDoc.LoadXml(path);
164              XmlNodeList nodeList = xmlDoc.SelectSingleNode(node).ChildNodes;//item 节点
165  
166              List<string> list = null;
167              Dictionary<int, List<string>> dic = new Dictionary<int, List<string>>();
168              int key = 0;
169              //遍历所有子节点
170              foreach (XmlNode xn in nodeList)
171              {
172                  XmlElement xe = (XmlElement)xn;  //item
173                  XmlNodeList subList = xe.ChildNodes;//item的子节点 
174  
175                  list = new List<string>();
176                  foreach (XmlNode xmlNode in subList)
177                  {
178                     //获取数据
179                      list.Add(xmlNode.InnerText);
180                  }
181                  dic.Add(key, list);
182                  key++;
183              }
184              return dic;
185          }                    
186     }
187 }                                                    

 

C#:XML操作类

标签:

原文地址:http://www.cnblogs.com/ssdnif/p/4718880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!