C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结.doc_第1页
C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结.doc_第2页
C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结.doc_第3页
C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结.doc_第4页
C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/打开某文件(假设web.config在根目录中) string filename=Server.MapPath(/) + WebApplication1web.config; XmlDocument xmldoc= new XmlDocument(); xmldoc.Load(filename); /得到顶层节点列表 XmlNodeList topM=xmldoc.DocumentElement.ChildNodes; foreach(XmlElement element in topM) if(element.Name.ToLower()=appsettings) /得到该节点的子节点 XmlNodeList nodelist=element.ChildNodes; if ( nodelist.Count 0 ) /DropDownList1.Items.Clear(); foreach(XmlElement el in nodelist)/读元素值 /DropDownList1.Items.Add(el.Attributeskey.InnerXml); /this.TextBox2.Text=el.Attributeskey.InnerText; this.TextBox2.Text=el.Attributeskey.Value; this.Label1.Text=el.Attributesvalue.Value; /同样在这里可以修改元素值,在后面save。 / el.Attributesvalue.Value=this.TextBox2.Text; xmldoc.Save(filename); 效果: /添加空元素: XmlNode node=doc.CreateElement(groupname); node.InnerText=; doc.LastChild.AppendChild(node); doc.Save(xmlfile);删除一个节点元素 string itemname=this.listBox1.SelectedItem.ToString(); this.listBox1.Items.Remove(this.listBox1.SelectedItem); /begin del xmlfile XmlDocument doc=new XmlDocument(); doc.Load(xmlfile); XmlNodeList topM=doc.DocumentElement.ChildNodes; foreach(XmlElement element in topM) if(element.Name=boBox1.Text) /得到该节点的子节点 XmlNodeList nodelist=element.ChildNodes; foreach(XmlElement el in nodelist)/读元素值 if(el.Attributeskey.Value=itemname) element.RemoveChild(el); /循环元素 /得到组 /循环组 doc.Save(xmlfile); /一定要保存一下,否则不起作用/筛选数据private void Reader_Xml(string pathFlie) XmlDocument Xmldoc=new XmlDocument(); Xmldoc.Load(pathFlie); XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Codeid=1) int f=0; foreach(XmlNode xnode in Record1) /*/*读取xml数据 两种xml方式*/ something something /*/*第一种方法*/DS.ReadXml(your xmlfile name);Container.DataItem(bb);Container.DataItem(cc);DS.ReadXmlSchema(your xmlfile name);/*/*第二种方法*/ 如果我要找到123然后取到321应该怎么写呢?using System.XML;XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();xmlDoc.Load(c:Config.xml);XmlElement elem = xmlDoc.GetElementById(add);string str = elem.Attributesvalue.Value/*/*第三种方法: SelectSingleNode 读取两种格式的xml *-/- Data Source=yf; user id=ctm_dbo;password=123在某节点下增加一个元素,并设置值: if(element.Name.ToLower()=appsettings) XmlElement elem =xmldoc.CreateElement(add); element.AppendChild(elem); elem.InnerText=ltp; xmldoc.Save(filename); 效果: ltp在某节点下增加一个元素,并增加两个属性: if(element.Name.ToLower()=appsettings) XmlElement elem =xmldoc.CreateElement(add); element.AppendChild(elem); XmlAttribute xa=xmldoc.CreateAttribute(key); xa.Value=ltp; XmlAttribute xa2=xmldoc.CreateAttribute(value); xa2.Value=first; elem.SetAttributeNode(xa); elem.SetAttributeNode(xa2); xmldoc.Save(filename);-XmlDocument doc = new XmlDocument();doc.Load(strXmlName); XmlNode node=doc.SelectSingleNode(/configuration/appSettings/ConnectionString); if(node!=null) string k1=node.Value; /null string k2=node.InnerText;/Data Source=yf; user id=ctm_dbo;password=123 string k3=node.InnerXml;/Data Source=yf; user id=ctm_dbo;password=123 node=null; * *-* XmlNode node=doc.SelectSingleNode(/configuration/appSettings/add); if(node!=null) string k=node.Attributeskey.Value; string v=node.Attributesvalue.Value; node=null; *-* XmlNode node=doc.SelectSingleNode(/configuration/appSettings/add); if(node!=null) Xml

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论