首页
网页制作
网络编程
脚本专栏
数据库
网站运营
网络安全
平面设计
CMS教程
搜索
建站极客
网络编程
ASP.NET
正文
读写xml所有节点个人小结 和 读取xml节点的数据总结
所属分类:
网络编程
/
ASP.NET
阅读数: 649
收藏 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;
}
更多精彩内容
其他人还在看
Asp.net图片上传实现预览效果的简单代码
这篇文章介绍了Asp.net图片上传实现预览效果的简单代码,有需要的朋友可以参考一下
评论 0
收藏 0
赞 0
分享
ASP.NET动态设置页面标题的方法详解
这篇文章介绍了ASP.NET动态设置页面标题的方法详解,有需要的朋友可以参考一下
评论 0
收藏 0
赞 0
分享
ASP.NET中获取URL重写前的原始地址详解
在ASP.NET中,如果你使用了URL重写,通过HttpContext.Request获取到的是重写后的地址。如果这个地址要返回给客户端(比如Redirect),我们一般希望是重写前的友好地址。
评论 0
收藏 0
赞 0
分享
.Net实现合并文件的具体方法
这篇文章介绍了.Net实现合并文件的具体方法,有需要的朋友可以参考一下
评论 0
收藏 0
赞 0
分享
asp.net 初始化文本框的小例子
这篇文章介绍了asp.net 初始化文本框的小例子,有需要的朋友可以参考一下,希望对你有所帮助
评论 0
收藏 0
赞 0
分享
用WPF实现屏幕文字提示的实现方法
本文介绍WPF应用程序实现在屏幕上显示一行或多行文字通知。它没有标题栏和最大化最小化等按钮,可以有半透明背景以使文字的显示更清晰,鼠标点击后提示消失。
评论 0
收藏 0
赞 0
分享
.NET更新Xml中CDATA内容的方法实例
这篇文章介绍了.NET更新Xml中CDATA内容的方法实例,有需要的朋友可以参考一下
评论 0
收藏 0
赞 0
分享
.NET中弹出对话框的方法汇总
下面是本人对常用对话框使用的汇总,希望对大家有所帮助,同时也欢迎大家补充。
评论 0
收藏 0
赞 0
分享
ASP.NET动态生成静态页面的实例代码
生成静态页有很多好处,可以缓解服务器压力、方便搜索网站搜索等等,下面介绍一下生成静态页的实例代码,有需要的朋友可以参考一下
评论 0
收藏 0
赞 0
分享
利用.net控件实现下拉导航菜单制作的具体方法
这篇文章介绍了利用.net控件实现下拉导航菜单制作的具体方法,有需要的朋友可以参考一下,希望对你有所帮助
评论 0
收藏 0
赞 0
分享
查看更多
网络赚钱
更多
内容型网站实现盈利的6种方法
揭秘朋友圈面膜:足不出户 国内生产俩月造出国际大牌
四年的网赚经验
一个既能省钱又能赚钱的公众号 你要错过么
816京东免单活动内容、规则 免单攻略
SEO中到底是流量重要还是转化率重要
站长故事
更多
站长故事之一份月薪1200元的SEO推广工作
创业失败:一个屌丝站长4年互联网的辛酸经历
求伯君:如果时光倒流我不会选择创业
人物:戴志康,80后创业者携惊叹号而来
暴走漫画运营经理 这就是高级运营与初级运营的区别
90后男子新婚杀妻后将照片发朋友圈续:被判死缓