A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  1. 读:
  2. //打开某文件(假设web.config在根目录中)
  3. string filename=Server.MapPath("/") + @"WebApplication1\web.config";
  4. XmlDocument xmldoc= new XmlDocument();
  5. xmldoc.Load(filename);

  6. //得到顶层节点列表
  7. XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
  8. foreach(XmlElement element in topM)
  9. {
  10. if(element.Name.ToLower()=="appsettings")
  11. {

  12. //得到该节点的子节点
  13. XmlNodeList nodelist=element.ChildNodes;

  14. if ( nodelist.Count >0 )
  15. {
  16. //DropDownList1.Items.Clear();

  17. foreach(XmlElement el in nodelist)//读元素值
  18. {
  19. //DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
  20. //this.TextBox2.Text=el.Attributes["key"].InnerText;
  21. this.TextBox2.Text=el.Attributes["key"].Value;
  22. this.Label1.Text=el.Attributes["value"].Value;


  23. //同样在这里可以修改元素值,在后面save。
  24. // el.Attributes["value"].Value=this.TextBox2.Text;
  25. }


  26. }

  27. }

  28. }

  29. xmldoc.Save(filename);

  30. 在某节点下增加一个元素,并设置值:

  31. if(element.Name.ToLower()=="appsettings")
  32. {

  33. XmlElement elem =xmldoc.CreateElement("add");

  34. element.AppendChild(elem);
  35. elem.InnerText="ltp";

  36. xmldoc.Save(filename);

  37. }

  38. 效果:
  39. <appSettings>
  40. <add key="密码" value="admin" />
  41. <add>ltp</add>
  42. </appSettings>

  43. 在某节点下增加一个元素,并增加两个属性:
  44. if(element.Name.ToLower()=="appsettings")
  45. {

  46. XmlElement elem =xmldoc.CreateElement("add");
  47. element.AppendChild(elem);

  48. XmlAttribute xa=xmldoc.CreateAttribute("key");
  49. xa.Value="ltp";

  50. XmlAttribute xa2=xmldoc.CreateAttribute("value");
  51. xa2.Value="first";

  52. elem.SetAttributeNode(xa);
  53. elem.SetAttributeNode(xa2);


  54. xmldoc.Save(filename);

  55. }

  56. 效果:
  57. <appSettings>
  58. <add key="密码" value="admin" />
  59. <add key="ltp" value="first" />
  60. </appSettings>

  61. //添加空元素:
  62. XmlNode node=doc.CreateElement(groupname);
  63. node.InnerText="";
  64. doc.LastChild.AppendChild(node);

  65. doc.Save(xmlfile);

  66. 删除一个节点元素
  67. string itemname=this.listBox1.SelectedItem.ToString();

  68. this.listBox1.Items.Remove(this.listBox1.SelectedItem);

  69. //begin del xmlfile
  70. XmlDocument doc=new XmlDocument();
  71. doc.Load(xmlfile);

  72. XmlNodeList topM=doc.DocumentElement.ChildNodes;
  73. foreach(XmlElement element in topM)
  74. {
  75. if(element.Name==this.comboBox1.Text)
  76. {

  77. //得到该节点的子节点
  78. XmlNodeList nodelist=element.ChildNodes;

  79. foreach(XmlElement el in nodelist)//读元素值
  80. {
  81. if(el.Attributes["key"].Value==itemname)
  82. {
  83. element.RemoveChild(el);
  84. }

  85. }//循环元素

  86. }//得到组

  87. }//循环组

  88. doc.Save(xmlfile); //一定要保存一下,否则不起作用

  89. //筛选数据
  90. private void Reader_Xml(string pathFlie)
  91. {
  92. XmlDocument Xmldoc=new XmlDocument();
  93. Xmldoc.Load(pathFlie);
  94. XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1'])
  95. int f=0;
  96. foreach(XmlNode xnode in Record1)
  97. {

  98. }
  99. } /**//*读取xml数据 两种xml方式*/
  100. <aaa>
  101. <bb>something</bb>
  102. <cc>something</cc>
  103. </aaa>

  104. <aaa>
  105. <add key="123" value="321"/>
  106. </aaa>

  107. /**//*第一种方法*/
  108. DS.ReadXml("your xmlfile name");
  109. Container.DataItem("bb");
  110. Container.DataItem("cc");
  111. DS.ReadXmlSchema("your xmlfile name");

  112. /**//*第二种方法*/
  113. <aaa>
  114. <add key="123" value="321"/>
  115. </aaa>
  116. 如果我要找到123然后取到321应该怎么写呢?

  117. using System.XML;
  118. XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
  119. xmlDoc.Load(@"c:\Config.xml");
  120. XmlElement elem = xmlDoc.GetElementById("add");
  121. string str = elem.Attributes["value"].Value


  122. /**//*第三种方法: SelectSingleNode 读取两种格式的xml *---/
  123. --------------------------------------------------------------------
  124. <?xml version="1.0" encoding="utf-8" ?>
  125. <configuration>
  126. <appSettings>
  127. <ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>
  128. </appSettings>
  129. </configuration>
  130. --------------------------------------------------------------------------
  131. XmlDocument doc = new XmlDocument();
  132. doc.Load(strXmlName);

  133. XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
  134. if(node!=null)
  135. {
  136. string k1=node.Value; //null
  137. string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
  138. string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
  139. node=null;
  140. }

  141. ********************************************************************
  142. <?xml version="1.0" encoding="utf-8" ?>
  143. <configuration>
  144. <appSettings>
  145. <add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />
  146. </appSettings>
  147. </configuration>
  148. **--------------------------------------------------------------------**
  149. XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
  150. if(node!=null)
  151. {
  152. string k=node.Attributes["key"].Value;
  153. string v=node.Attributes["value"].Value;
  154. node=null;
  155. }
  156. *--------------------------------------------------------------------*
  157. XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
  158. if(node!=null)
  159. {
  160. XmlNodeReader nr=new XmlNodeReader(node);
  161. nr.MoveToContent();
  162. //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
  163. nr.MoveToAttribute("value");
  164. string s=nr.Value;
  165. node=null;
  166. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马