读写xml所有节点个人小结 和 读取xml节点的数据总结

所属分类: 网络编程 / ASP.NET 阅读数: 660
收藏 0 赞 0 分享
读: 
    
//打开某文件(假设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);
       
    }

 
效果:
  
<appSettings>
    
<add key="密码" value="admin" />
    
<add>ltp</add>
  
</appSettings>
 
在某节点下增加一个元素,并增加两个属性:
    
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);
       
    }

 
效果:
  
<appSettings>
    
<add key="密码" value="admin" />
    
<add key="ltp" value="first" />
  
</appSettings>
 
//添加空元素:
   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方式*/
<aaa>
     
<bb>something</bb>
     
<cc>something</cc>
</aaa>
 
<aaa>
    
<add key="123" value="321"/>
</aaa>

/*第一种方法*/
DS.ReadXml(
"your xmlfile name");
Container.DataItem(
"bb");
Container.DataItem(
"cc");
DS.ReadXmlSchema(
"your xmlfile name");
 
/*第二种方法*/
<aaa>
    
<add key="123" value="321"/>
</aaa>
如果我要找到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 *---/
--------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
       <ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>             
  </appSettings>
</configuration>
--------------------------------------------------------------------------
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;
    }
 
********************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
       <add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />             
  </appSettings>
</configuration>
**--------------------------------------------------------------------**
     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;
    }
更多精彩内容其他人还在看

解析WPF实现音频文件循环顺序播放的解决方法

本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解

本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

将文件上传、下载(以二进制流保存到数据库)实现代码

将文件以二进制流的格式写入数据库:首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中具体请祥看本文,希望对你有所帮助
收藏 0 赞 0 分享

点击提交按钮后DropDownList的值变为默认值实现分析

在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值
收藏 0 赞 0 分享

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Linkbutton控件在项目中的简单应用

Button控件可分为button控件、LinkButton控件、ImageButton控件三类,而LinkButton控件则在页面上显示为一个超级链接,下面与大家分享下其具体应用
收藏 0 赞 0 分享

Web.config 和 App.config 的区别分析

Web.config 和 App.config 的区别分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于.Net中的数字与日期格式化规则助记词的使用详解

本篇文章是对.Net中的数字与日期格式化规则助记词的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决在Web.config或App.config中添加自定义配置的方法详解

本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入本机影像生成器(Ngen.exe)工具使用方法详解

本篇文章是对本机影像生成器(Ngen.exe)工具使用方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多