操作XML--C#

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Text;

  4 using System.Xml;

  5 using System.Data;

  6 

  7 namespace operate_xml

  8 {

  9     class operate_xml1

 10     {

 11         public XmlDocument myXmlDocument;

 12 

 13         /// <summary>

 14         /// 带参构造函数

 15         /// </summary>

 16         /// <param name="xml_name">Xml的路径</param>

 17         public operate_xml1(string xml_name)

 18         {

 19             myXmlDocument = new XmlDocument();

 20             myXmlDocument.Load(xml_name);

 21         }

 22         /// <summary>

 23         /// 更新节点

 24         /// </summary>

 25         /// <param name="xml_name"></param>

 26         /// <param name="name"></param>

 27         /// <param name="value"></param>

 28         /// <param name="weapon"></param>

 29         /// <returns></returns>

 30         public bool updata_xml(string xml_name,string name,string value,string weapon)

 31         {

 32             bool isSuccess = false;

 33             XmlNode node;           

 34            

 35             node = myXmlDocument.DocumentElement; //获取XML文档的根

 36             try

 37             {

 38                 foreach (XmlNode node1 in node.ChildNodes)

 39                     foreach (XmlNode node2 in node1.ChildNodes)

 40                     {

 41                         if (node2.InnerText == name)

 42                         {

 43                             node2.NextSibling.InnerText = value;        //获取下一个节点

 44                             node2.NextSibling.NextSibling.InnerText = weapon;   //获取下一个节点

 45                             isSuccess = true;                       

 46                         }

 47                     }

 48                 myXmlDocument.Save(xml_name);

 49                

 50             }

 51             catch (Exception ex)

 52             {               

 53             }

 54             return isSuccess;

 55         }

 56         /// <summary>

 57         /// 添加节点

 58         /// </summary>

 59         /// <param name="xml_name"></param>

 60         /// <param name="name"></param>

 61         /// <param name="power"></param>

 62         /// <param name="weapon"></param>

 63         /// <returns></returns>

 64         public bool CreatNode(string xml_name,string name,string power,string weapon)

 65         {

 66             bool isSuccess = false;

 67             XmlNode node;

 68             node = myXmlDocument.DocumentElement;

 69             try

 70             {

 71                 XmlElement subElement = myXmlDocument.createElement_x("body");

 72                

 73 

 74                 XmlElement subElement1 = myXmlDocument.createElement_x("name");

 75                 subElement1.InnerText = name;

 76                 XmlElement subElement2 = myXmlDocument.createElement_x("power");

 77                 subElement2.InnerText = power;

 78                 XmlElement subElement3 = myXmlDocument.createElement_x("weapon");

 79                 subElement3.InnerText = weapon;

 80 

 81                 subElement.AppendChild(subElement1);

 82                 subElement.AppendChild(subElement2);

 83                 subElement.AppendChild(subElement3);

 84 

 85                 node.AppendChild(subElement);

 86                 myXmlDocument.Save(xml_name);

 87 

 88                 isSuccess = true;

 89             }

 90             catch (Exception ex)

 91             { }

 92             return isSuccess;

 93         }

 94         /// <summary>

 95         /// 删除节点,第一种方法

 96         /// </summary>

 97         /// <param name="xml_name"></param>

 98         /// <param name="name"></param>

 99         /// <returns></returns>

100         public bool DeleteNode(string xml_name, string name)

101         {

102             bool isSuccess = false;

103             XmlNode node;

104             node = myXmlDocument.DocumentElement;

105             try

106             {

107                 foreach (XmlNode node1 in node.ChildNodes)

108                     foreach (XmlNode node2 in node1.ChildNodes)

109                         if (node2.InnerText == name)

110                             node2.ParentNode.RemoveAll();

111                 myXmlDocument.Save(xml_name);   //记得要保存

112                 isSuccess = true;

113             }

114             catch (Exception ex)

115             { }

116             return isSuccess;

117         }

118         /// <summary>

119         /// 删除节点,第二种方法

120         /// </summary>

121         /// <param name="xml_name"></param>

122         /// <param name="name"></param>

123         /// <returns></returns>

124         public bool DeleteNode1(string xml_name, string name)

125         {

126             bool isSuccess = false;

127             XmlNode node1;

128             XmlNodeList nodelist = myXmlDocument.SelectNodes("histroy/body/name");   //选择所有匹配name的节点

129             foreach (XmlNode node in nodelist)

130             {              

131                     if (node.InnerText == name)

132                     {

133                         node1 = node.ParentNode;

134                         node1.ParentNode.RemoveChild(node1);

135                         isSuccess = true;

136                     }               

137             }

138             myXmlDocument.Save(xml_name);

139             return isSuccess;

140         }

141         /// <summary>

142         /// 查询人物

143         /// </summary>

144         /// <param name="name"></param>

145         /// <returns></returns>

146         public XmlNode getxmlnode(string name)

147         {

148             XmlNode xmlNode=null;           

149             XmlNodeList nodelist = myXmlDocument.SelectNodes("//histroy//body//name");   //选择所有匹配name的节点

150             foreach (XmlNode node in nodelist)

151             {

152                 if (node.InnerText == name)

153                 {

154                     xmlNode = node.ParentNode;                   

155                 }

156             }

157             return xmlNode;

158         }

159         /// <summary>

160         /// 使用dataset显示整个XML文件

161         /// </summary>

162         /// <returns></returns>

163         public DataSet GetDs()

164         {

165             DataSet ds = new DataSet();

166             try

167             {

168                 System.IO.StringReader read = new System.IO.StringReader(myXmlDocument.SelectSingleNode("//histroy").OuterXml);

169                 ds.ReadXml(read);

170                 read.Close();

171             }

172             catch

173             { }

174             return ds;

175         }

176 

177    }

178 

179 }

 

原文:http://blog.sina.com.cn/s/blog_66e765d90100j0az.html

你可能感兴趣的:(xml)