读写xml所有节点个人小结 和 读取xml节点的数据总结
来源:互联网 发布:企业秀是个什么软件 编辑:程序博客网 时间:2024/05/21 22:37
读: //打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1/web.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.Attributes["key"].InnerXml); //this.TextBox2.Text=el.Attributes["key"].InnerText; this.TextBox2.Text=el.Attributes["key"].Value; this.Label1.Text=el.Attributes["value"].Value; //同样在这里可以修改元素值,在后面save。 // el.Attributes["value"].Value=this.TextBox2.Text; } } } } xmldoc.Save(filename); 在某节点下增加一个元素,并设置值: 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); } 效果: //添加空元素: 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==this.comboBox1.Text) {
//得到该节点的子节点 XmlNodeList nodelist=element.ChildNodes; foreach(XmlElement el in nodelist)//读元素值
{ if(el.Attributes["key"].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(Code[@id='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.Attributes["value"].Value
/**//*第三种方法: SelectSingleNode 读取两种格式的xml *---/
--------------------------------------------------------------------
Data Source=yf; user id=ctm_dbo;password=123
-------------------------------------------------------------------------- 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.Attributes["key"].Value; string v=node.Attributes["value"].Value; node=null; }
*--------------------------------------------------------------------*
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add"); if(node!=null) { XmlNodeReader nr=new XmlNodeReader(node); nr.MoveToContent(); //检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。 nr.MoveToAttribute("value"); string s=nr.Value; node=null; }
http://www.cnblogs.com/skylaugh/archive/2006/12/18/595637.html --原网址
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结和读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- 读写xml所有节点小结
- 读取xml节点的数据总结
- 读取xml节点的数据总结
- 读取xml节点的数据总结
- 读取xml所有节点
- 注意商品位,的类型保留字符
- 设置对话框显示位置
- Ubuntu Firefox不能显示flash的解决方案
- 联通正式发布沃Phone手机操作系统 称拥有自主产权
- Unicode编码汉字对照表(二)
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- ext combo 无法获取VALUE值
- Windows CE 编程的十点忠告
- nginx 502 Bad Gateway 的一种解决方法(转载)
- Apache Commons工具集简介
- CSDN博客字体设置,字体颜色设置失效
- C# Winform 去掉按钮按下时周围黑线框
- C++ 简单的文件操作_适合初学者
- FlashDevelp3.3.4+Molehill_alpha+Away3D4_alpha配置环境下的水波纹Demo编译