c#对xml的简单操作

所属分类: 网络编程 / ASP.NET 阅读数: 1985
收藏 0 赞 0 分享
xml文件格式如下: 
<?xml version="1.0" encoding="utf-8"?> 
<userdata createuser="false"> 
<dataconnection> 
<server>localhost</server> 
<uid>sa</uid> 
<pwd></pwd> 
</dataconnection> 
<net> 
<name>jiayuan</name> 
</net> 
</userdata> 

读取节点中的一个属性 
XmlDocument doc=new XmlDocument(); 
doc.Load("config.xml");//可以再加入路径:如D:\config.xml 
XmlNode xnuser=doc.SelectSingleNode("userdata"); 
string flag=xnuser.Attributes["createuser"].InnerText; 

读取节点中的值 
XmlDocument doc=new XmlDocument(); 
doc.Load("config.xml"); 
XmlNode xnserver = doc.SelectSingleNode("userdata/dataconnection/server"); 

修改节点的属性 
XmlDocument doc=new XmlDocument(); 
doc.Load("config.xml"); 
XmlNode xnuser=doc.SelectSingleNode("userdata"); 
xnuser.Attributes["createuser"].InnerText="false"; 
doc.Save("config.xml"); 

追加节点 
XmlDocument doc = new XmlDocument(); 
XmlTextReader reader = new XmlTextReader("config.xml"); 
doc.Load("config.xml"); 
XmlElement root = doc.DocumentElement; // 获取根节点 
XmlElement tagMessage = doc.CreateElement("net"); 
XmlElement tagText = doc.CreateElement("name"); 
tagText.InnerText = netname; 
tagMessage.AppendChild(tagText); // 追加到 xml 文本的最后面 
root.AppendChild(tagMessage); 
reader.Close(); // 关闭 XmlTextReader 
doc.Save("config.xml"); // 保存 xml 文件 
更多精彩内容其他人还在看

解析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 分享
查看更多