本文属于网摘
1 一、简单介绍 2 using System.Xml; 3 //初始化一个xml实例 4 XmlDocument xml=new XmlDocument(); 5 //导入指定xml文件 6 xml.Load(path); 7 xml.Load(HttpContext.Current.Server.MapPath(@H_301_60@"@H_301_60@~/file/bookstore.xml@H_301_60@")); 8 //指定一个节点 9 XmlNode root=xml.SelectSingleNode(@H_301_60@"@H_301_60@/root@H_301_60@"); 10 //获取节点下所有直接子节点 11 XmlNodeList childlist=root.ChildNodes; 12 //判断该节点下是否有子节点 13 root.HasChildNodes; 14 //获取同名同级节点集合 15 XmlNodeList nodelist=xml.SelectNodes(@H_301_60@"@H_301_60@/Root/News@H_301_60@"); 16 //生成一个新节点 17 XmlElement node=xml.createElement_x(@H_301_60@"@H_301_60@News@H_301_60@"); 18 //将节点加到指定节点下,作为其子节点 19 root.AppendChild(node); 20 //将节点加到指定节点下某个子节点前 21 root.InsertBefore(node,root.ChildeNodes[i]); 22 //为指定节点的新建属性并赋值 23 node.SetAttribute(@H_301_60@"@H_301_60@id@H_301_60@",@H_301_60@"@H_301_60@11111@H_301_60@"); 24 //为指定节点添加子节点 25 root.AppendChild(node); 26 //获取指定节点的指定属性值 27 string id=node.Attributes[@H_301_60@"@H_301_60@id@H_301_60@"].Value; 28 //获取指定节点中的文本 29 string content=node.InnerText; 30 //保存XML文件 31 string path=Server.MapPath(@H_301_60@"@H_301_60@~/file/bookstore.xml@H_301_60@"); 32 xml.Save(path); 33 //or use :xml.Save(HttpContext.Current.Server.MapPath("~/file/bookstore.xml")); 34 二、具体实例 35 在C#.net中如何操作XML 36 需要添加的命名空间: 37 using System.Xml; 38 定义几个公共对象: 39 XmlDocument xmldoc ; 40 XmlNode xmlnode ; 41 XmlElement xmlelem ; 42 1,创建到服务器同名目录下的xml文件: 43 44 方法一: 45 xmldoc = new XmlDocument ( ) ; 46 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?> 47 XmlDeclaration xmldecl; 48 xmldecl = xmldoc.CreateXmlDeclaration(@H_301_60@"@H_301_60@1.0@H_301_60@",@H_301_60@"@H_301_60@gb2312@H_301_60@",null); 49 xmldoc.AppendChild ( xmldecl); 50 //加入一个根元素 51 xmlelem = xmldoc.createElement_x ( @H_301_60@"" , @H_301_60@"@H_301_60@Employees@H_301_60@" , @H_301_60@"" ) ; 52 xmldoc.AppendChild ( xmlelem ) ; 53 //加入另外一个元素 54 for(int i=1;i<3;i++) 55 { 56 XmlNode root=xmldoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@");//查找<Employees> 57 XmlElement xe1=xmldoc.createElement_x(@H_301_60@"@H_301_60@Node@H_301_60@");//创建一个<Node>节点 58 xe1.SetAttribute(@H_301_60@"@H_301_60@genre@H_301_60@",@H_301_60@"@H_301_60@李赞红@H_301_60@");//设置该节点genre属性 59 xe1.SetAttribute(@H_301_60@"@H_301_60@ISBN@H_301_60@",@H_301_60@"@H_301_60@2-3631-4@H_301_60@");//设置该节点ISBN属性 60 XmlElement xesub1=xmldoc.createElement_x(@H_301_60@"@H_301_60@title@H_301_60@"); 61 xesub1.InnerText=@H_301_60@"@H_301_60@CS从入门到精通@H_301_60@";//设置文本节点 62 xe1.AppendChild(xesub1);//添加到<Node>节点中 63 XmlElement xesub2=xmldoc.createElement_x(@H_301_60@"@H_301_60@author@H_301_60@"); 64 xesub2.InnerText=@H_301_60@"@H_301_60@候捷@H_301_60@"; 65 xe1.AppendChild(xesub2); 66 XmlElement xesub3=xmldoc.createElement_x(@H_301_60@"@H_301_60@price@H_301_60@"); 67 xesub3.InnerText=@H_301_60@"@H_301_60@58.3@H_301_60@"; 68 xe1.AppendChild(xesub3); 69 root.AppendChild(xe1);//添加到<Employees>节点中 70 } 71 //保存创建好的XML文档 72 xmldoc.Save ( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ) ; 73////////////////////////////////////////////////////////////////////////////////////// 74 结果:在同名目录下生成了名为data.xml的文件,内容如下, 75 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 76 <Employees> 77 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 78 <title>CS从入门到精通</title> 79 <author>候捷</author> 80 <price>58.3</price> 81 </Node> 82 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 83 <title>CS从入门到精通</title> 84 <author>候捷</author> 85 <price>58.3</price> 86 </Node> 87 </Employees> 88 89 方法二: 90 XmlTextWriter xmlWriter; 91 string strFilename = Server.MapPath(@H_301_60@"@H_301_60@data1.xml@H_301_60@") ; 92 xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档 93 xmlWriter.Formatting = Formatting.Indented; 94 xmlWriter.WriteStartDocument(); 95 xmlWriter.WriteStartElement(@H_301_60@"@H_301_60@Employees@H_301_60@"); 96 xmlWriter.WriteStartElement(@H_301_60@"@H_301_60@Node@H_301_60@"); 97 xmlWriter.WriteAttributeString(@H_301_60@"@H_301_60@genre@H_301_60@",@H_301_60@"@H_301_60@李赞红@H_301_60@"); 98 xmlWriter.WriteAttributeString(@H_301_60@"@H_301_60@ISBN@H_301_60@",@H_301_60@"@H_301_60@2-3631-4@H_301_60@"); 99 xmlWriter.WriteStartElement(@H_301_60@"@H_301_60@title@H_301_60@"); 100 xmlWriter.WriteString(@H_301_60@"@H_301_60@CS从入门到精通@H_301_60@"); 101 xmlWriter.WriteEndElement(); 102 xmlWriter.WriteStartElement(@H_301_60@"@H_301_60@author@H_301_60@"); 103 xmlWriter.WriteString(@H_301_60@"@H_301_60@候捷@H_301_60@"); 104 xmlWriter.WriteEndElement(); 105 xmlWriter.WriteStartElement(@H_301_60@"@H_301_60@price@H_301_60@"); 106 xmlWriter.WriteString(@H_301_60@"@H_301_60@58.3@H_301_60@"); 107 xmlWriter.WriteEndElement(); 108 xmlWriter.WriteEndElement(); 109 xmlWriter.Close(); 110////////////////////////////////////////////////////////////////////////////////////// 111 结果: 112 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 113 <Employees> 114 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 115 <title>CS从入门到精通</title> 116 <author>候捷</author> 117 <price>58.3</price> 118 </Node> 119 </Employees> 120 2,添加一个结点: 121 XmlDocument xmlDoc=new XmlDocument(); 122 xmlDoc.Load(Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@")); 123 XmlNode root=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@");//查找<Employees> 124 XmlElement xe1=xmlDoc.createElement_x(@H_301_60@"@H_301_60@Node@H_301_60@");//创建一个<Node>节点 125 xe1.SetAttribute(@H_301_60@"@H_301_60@genre@H_301_60@",@H_301_60@"@H_301_60@张三@H_301_60@");//设置该节点genre属性 126 xe1.SetAttribute(@H_301_60@"@H_301_60@ISBN@H_301_60@",@H_301_60@"@H_301_60@1-1111-1@H_301_60@");//设置该节点ISBN属性 127 XmlElement xesub1=xmlDoc.createElement_x(@H_301_60@"@H_301_60@title@H_301_60@"); 128 xesub1.InnerText=@H_301_60@"@H_301_60@C#入门帮助@H_301_60@";//设置文本节点 129 xe1.AppendChild(xesub1);//添加到<Node>节点中 130 XmlElement xesub2=xmlDoc.createElement_x(@H_301_60@"@H_301_60@author@H_301_60@"); 131 xesub2.InnerText=@H_301_60@"@H_301_60@高手@H_301_60@"; 132 xe1.AppendChild(xesub2); 133 XmlElement xesub3=xmlDoc.createElement_x(@H_301_60@"@H_301_60@price@H_301_60@"); 134 xesub3.InnerText=@H_301_60@"@H_301_60@158.3@H_301_60@"; 135 xe1.AppendChild(xesub3); 136 root.AppendChild(xe1);//添加到<Employees>节点中 137 xmlDoc.Save ( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 138////////////////////////////////////////////////////////////////////////////////////// 139 结果:在xml原有的内容里添加了一个结点,内容如下, 140 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 141 <Employees> 142 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 143 <title>CS从入门到精通</title> 144 <author>候捷</author> 145 <price>58.3</price> 146 </Node> 147 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 148 <title>CS从入门到精通</title> 149 <author>候捷</author> 150 <price>58.3</price> 151 </Node> 152 <Node genre=@H_301_60@"@H_301_60@张三@H_301_60@" ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@"> 153 <title>C#入门帮助</title> 154 <author>高手</author> 155 <price>158.3</price> 156 </Node> 157 </Employees> 158 3,修改结点的值(属性和子结点): 159 XmlDocument xmlDoc=new XmlDocument(); 160 xmlDoc.Load( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 161 XmlNodeList nodeList=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@").ChildNodes;//获取Employees节点的所有子节点 162 foreach(XmlNode xn in nodeList)//遍历所有子节点 163 { 164 XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 165 if(xe.GetAttribute(@H_301_60@"@H_301_60@genre@H_301_60@")==@H_301_60@"@H_301_60@张三@H_301_60@")//如果genre属性值为“张三” 166 { 167 xe.SetAttribute(@H_301_60@"@H_301_60@genre@H_301_60@",@H_301_60@"@H_301_60@update张三@H_301_60@");//则修改该属性为“update张三” 168 XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 169 foreach(XmlNode xn1 in nls)//遍历 170 { 171 XmlElement xe2=(XmlElement)xn1;//转换类型 172 if(xe2.Name==@H_301_60@"@H_301_60@author@H_301_60@")//如果找到 173 { 174 xe2.InnerText=@H_301_60@"@H_301_60@亚胜@H_301_60@";//则修改 175 } 176 } 177 } 178 } 179 xmlDoc.Save( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") );//保存。 180////////////////////////////////////////////////////////////////////////////////////// 181 结果:将原来的所有结点的信息都修改了,xml的内容如下, 182 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 183 <Employees> 184 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 185 <title>CS从入门到精通</title> 186 <author>候捷</author> 187 <price>58.3</price> 188 </Node> 189 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 190 <title>CS从入门到精通</title> 191 <author>候捷</author> 192 <price>58.3</price> 193 </Node> 194 <Node genre=@H_301_60@"@H_301_60@update张三@H_301_60@" ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@"> 195 <title>C#入门帮助</title> 196 <author>亚胜</author> 197 <price>158.3</price> 198 </Node> 199 </Employees> 200 4,修改结点(添加结点的属性和添加结点的自结点): 201 XmlDocument xmlDoc=new XmlDocument(); 202 xmlDoc.Load( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 203 XmlNodeList nodeList=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@").ChildNodes;//获取Employees节点的所有子节点 204 foreach(XmlNode xn in nodeList) 205 { 206 XmlElement xe=(XmlElement)xn; 207 xe.SetAttribute(@H_301_60@"@H_301_60@test@H_301_60@",@H_301_60@"@H_301_60@111111@H_301_60@"); 208 XmlElement xesub=xmlDoc.createElement_x(@H_301_60@"@H_301_60@flag@H_301_60@"); 209 xesub.InnerText=@H_301_60@"@H_301_60@1@H_301_60@"; 210 xe.AppendChild(xesub); 211 } 212 xmlDoc.Save( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 213////////////////////////////////////////////////////////////////////////////////////// 214 结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下, 215 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 216 <Employees> 217 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 218 <title>CS从入门到精通</title> 219 <author>候捷</author> 220 <price>58.3</price> 221 <flag>1</flag> 222 </Node> 223 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 224 <title>CS从入门到精通</title> 225 <author>候捷</author> 226 <price>58.3</price> 227 <flag>1</flag> 228 </Node> 229 <Node genre=@H_301_60@"@H_301_60@update张三@H_301_60@" ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 230 <title>C#入门帮助</title> 231 <author>亚胜</author> 232 <price>158.3</price> 233 <flag>1</flag> 234 </Node> 235 </Employees> 236 5,删除结点中的某一个属性: 237 XmlDocument xmlDoc=new XmlDocument(); 238 xmlDoc.Load( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 239 XmlNodeList xnl=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@").ChildNodes; 240 foreach(XmlNode xn in xnl) 241 { 242 XmlElement xe=(XmlElement)xn; 243 xe.RemoveAttribute(@H_301_60@"@H_301_60@genre@H_301_60@");//删除genre属性 244 XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 245 foreach(XmlNode xn1 in nls)//遍历 246 { 247 XmlElement xe2=(XmlElement)xn1;//转换类型 248 if(xe2.Name==@H_301_60@"@H_301_60@flag@H_301_60@")//如果找到 249 { 250 xe.RemoveChild(xe2);//则删除 251 } 252 } 253 } 254 xmlDoc.Save( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 255//////////////////////////////////////////////////////////////////////////////////////] 256 结果:删除了结点的一个属性和结点的一个子结点,内容如下, 257 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 258 <Employees> 259 <Node ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 260 <title>CS从入门到精通</title> 261 <author>候捷</author> 262 <price>58.3</price> 263 </Node> 264 <Node ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 265 <title>CS从入门到精通</title> 266 <author>候捷</author> 267 <price>58.3</price> 268 </Node> 269 <Node ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@" test=@H_301_60@"@H_301_60@111111@H_301_60@"> 270 <title>C#入门帮助</title> 271 <author>亚胜</author> 272 <price>158.3</price> 273 </Node> 274 </Employees> 275 6,删除结点: 276 XmlDocument xmlDoc=new XmlDocument(); 277 xmlDoc.Load( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 278 XmlNode root=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@"); 279 XmlNodeList xnl=xmlDoc.SelectSingleNode(@H_301_60@"@H_301_60@Employees@H_301_60@").ChildNodes; 280 for(int i=0;i<xnl.Count;i++) 281 { 282 XmlElement xe=(XmlElement)xnl.Item(i); 283 if(xe.GetAttribute(@H_301_60@"@H_301_60@genre@H_301_60@")==@H_301_60@"@H_301_60@张三@H_301_60@") 284 { 285 root.RemoveChild(xe); 286 if(i<xnl.Count)i=i-1; 287 } 288 } 289 xmlDoc.Save( Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@") ); 290//////////////////////////////////////////////////////////////////////////////////////] 291 结果:删除了符合条件的所有结点,原来的内容: 292 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 293 <Employees> 294 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 295 <title>CS从入门到精通</title> 296 <author>候捷</author> 297 <price>58.3</price> 298 </Node> 299 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 300 <title>CS从入门到精通</title> 301 <author>候捷</author> 302 <price>58.3</price> 303 </Node> 304 <Node genre=@H_301_60@"@H_301_60@张三@H_301_60@" ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@"> 305 <title>C#入门帮助</title> 306 <author>高手</author> 307 <price>158.3</price> 308 </Node> 309 <Node genre=@H_301_60@"@H_301_60@张三@H_301_60@" ISBN=@H_301_60@"@H_301_60@1-1111-1@H_301_60@"> 310 <title>C#入门帮助</title> 311 <author>高手</author> 312 <price>158.3</price> 313 </Node> 314 </Employees> 315 删除后的内容: 316 <?xml version=@H_301_60@"@H_301_60@1.0@H_301_60@" encoding=@H_301_60@"@H_301_60@gb2312@H_301_60@"?> 317 <Employees> 318 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 319 <title>CS从入门到精通</title> 320 <author>候捷</author> 321 <price>58.3</price> 322 </Node> 323 <Node genre=@H_301_60@"@H_301_60@李赞红@H_301_60@" ISBN=@H_301_60@"@H_301_60@2-3631-4@H_301_60@"> 324 <title>CS从入门到精通</title> 325 <author>候捷</author> 326 <price>58.3</price> 327 </Node> 328 </Employees> 329 7,按照文本文件读取xml 330 System.IO.StreamReader myFile =new 331 System.IO.StreamReader(Server.MapPath(@H_301_60@"@H_301_60@data.xml@H_301_60@"),System.Text.Encoding.Default); 332 //注意System.Text.Encoding.Default 333 string myString = myFile.ReadToEnd();//myString是读出的字符串 334 myFile.Close(); 335 336 三、高级应用 337 338 <aaa> 339 <bb>something</bb> 340 <cc>something</cc> 341 </aaa> 342 343 <aaa> 344 <add key=@H_301_60@"@H_301_60@123@H_301_60@" value=@H_301_60@"@H_301_60@321@H_301_60@"/> 345 </aaa> 346 347 DS.ReadXml(@H_301_60@"@H_301_60@your xmlfile name@H_301_60@"); 348 Container.DataItem(@H_301_60@"@H_301_60@bb@H_301_60@"); 349 Container.DataItem(@H_301_60@"@H_301_60@cc@H_301_60@"); 350 DS.ReadXmlSchema(@H_301_60@"@H_301_60@your xmlfile name@H_301_60@"); 351 352 353 <aaa> 354 <add key=@H_301_60@"@H_301_60@123@H_301_60@" value=@H_301_60@"@H_301_60@321@H_301_60@"/> 355 </aaa> 356 如果我要找到123然后取到321应该怎么写呢? 357 358 using System.XML; 359 XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(); 360 xmlDoc.Load(@H_301_60@@"@H_301_60@c:\Config.xml@H_301_60@"); 361 XmlElement elem = xmlDoc.GetElementById(@H_301_60@"@H_301_60@add@H_301_60@"); 362 string str = elem.Attributes[@H_301_60@"@H_301_60@value@H_301_60@"].Value 363 364 365 366 -------------------------------------------------------------------- 367 <?xml version="1.0" encoding="utf-8" ?> 368 <configuration> 369 <appSettings> 370 <ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString> 371 </appSettings> 372 </configuration> 373 -------------------------------------------------------------------------- 374 XmlDocument doc = new XmlDocument(); 375 doc.Load(strXmlName); 376 377 XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString"); 378 if(node!=null) 379 { 380 string k1=node.Value; //null 381 string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123 382 string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123 383 node=null; 384 } 385 386 ******************************************************************** 387 <?xml version="1.0" encoding="utf-8" ?> 388 <configuration> 389 <appSettings> 390 <add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" /> 391 </appSettings> 392 </configuration> 393 **--------------------------------------------------------------------** 394 XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add"); 395 if(node!=null) 396 { 397 string k=node.Attributes["key"].Value; 398 string v=node.Attributes["value"].Value; 399 node=null; 400 } 401 *--------------------------------------------------------------------* 402 XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add"); 403 if(node!=null) 404 { 405 XmlNodeReader nr=new XmlNodeReader(node); 406 nr.MoveToContent(); 407 //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。 408 nr.MoveToAttribute("value"); 409 string s=nr.Value; 410 node=null; 411 }