.net操纵xml文件类(c#)(2)
2008-02-22 09:38:24来源:互联网 阅读 ()
136 /// <returns></returns>
137 public string GetXmlNodeAttributeValue(string strNode,string strAttribute)
138 {
139 string strReturn = "";
140 try
141 {
142 //根据指定路径获取节点
143 XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
144 if (!(xmlNode==null))
145 {//获取节点的属性,并循环取出需要的属性值
146 XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
147
148 for(int i=0 ;i<xmlAttr.Count; i )
149 {
150 if (xmlAttr.Item(i).Name == strAttribute)
151 {
152 strReturn = xmlAttr.Item(i).Value ;
153 break;
154 }
155 }
156 }
157 }
158 catch(XmlException xmle)
159 {
160 throw xmle ;
161 }
162 return strReturn ;
163 }
164 #endregion
165
166
167 读取指定节点的值#region 读取指定节点的值
168 /**//// <summary>
169 /// 功能:
170 /// 读取指定节点的值
171 /// </summary>
172 /// <param name="strNode">节点名称</param>
173 /// <returns></returns>
174 public string GetXmlNodeValue(string strNode)
175 {
176 string strReturn = String.Empty ;
177
178 try
179 {
180 //根据路径获取节点
181 XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode) ;
182 if (!(xmlNode==null))
183 strReturn = xmlNode.InnerText ;
184 }
185 catch(XmlException xmle)
186 {
187 throw xmle ;
188 }
189 return strReturn ;
190 }
191 #endregion
192
193 设置节点值#region 设置节点值
194 /**//// <summary>
195 /// 功能:
196 /// 设置节点值
197 /// </summary>
198 /// <param name="strNode">节点的名称</param>
199 /// <param name="newValue">节点值</param>
200 public void SetXmlNodeValue(string xmlNodePath,string xmlNodeValue)
201 {
202 try
203 {
204 //可以批量为符合条件的节点进行付值
205 XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
206 if (!(xmlNode==null))
207 {
208 foreach(XmlNode xn in xmlNode)
209 {
210 xn.InnerText = xmlNodeValue ;
211 }
212 }
213 /**//*
214 * 根据指定路径获取节点
215 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
216 //设置节点值
217 if (!(xmlNode==null))
218 xmlNode.InnerText = xmlNodeValue ;*/
219 }
220 catch(XmlException xmle)
221 {
222 throw xmle ;
223 }
224 }
225 #endregion
226
227 设置节点的属性值#region 设置节点的属性值
228 /**//// <summary>
229 /// 功能:
230 /// 设置节点的属性值
231 /// </summary>
232 /// <param name="xmlNodePath">节点名称</param>
233 /// <param name="xmlNodeAttribute">属性名称</param>
234 /// <param name="xmlNodeAttributeValue">属性值</param>
235 public void SetXmlNodeAttributeValue(string xmlNodePath,string xmlNodeAttribute,string xmlNodeAttributeValue)
236 {
237 try
238 {
239 //可以批量为符合条件的节点的属性付值
240 XmlNodeList xmlNode=this.xmlDoc.SelectNodes(xmlNodePath);
241 if (!(xmlNode==null))
242 {
243 foreach(XmlNode xn in xmlNode)
244 {
245 XmlAttributeCollection xmlAttr = xn.Attributes ;
246 for(int i=0 ; i<xmlAttr.Count ; i )
247 {
248 if ( xmlAttr.Item(i).Name == xmlNodeAttribute )
249 {
250 xmlAttr.Item(i).Value = xmlNodeAttributeValue;
251 break ;
252 }
253 }
254 }
255 }
256 /**//*单个节点
257 //根据指定路径获取节点
258 XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
259 if (!(xmlNode==null))
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
