asp.net修改XML节点值
因为网站幻灯片是flash读取xml里面的数据。我想在后台加一个页面编辑,修改节点的值。嗯,XML如下:<?xmlversion="1.0"encoding="UTF-8...
因为网站幻灯片是flash读取xml里面的数据。我想在后台加一个页面编辑,修改节点的值。嗯,XML如下:
<?xml version="1.0" encoding="UTF-8" ?>
<images>
<i src="images/01.jpg" info="11" href="111"/>
<i src="images/02.jpg" info="22" href="222"/>
<i src="images/03.jpg" info="33" href="333"/>
<i src="images/04.jpg" info="44" href="444"/>
</images>
主要修改src、info、href的值,求cs代码 谢谢 展开
<?xml version="1.0" encoding="UTF-8" ?>
<images>
<i src="images/01.jpg" info="11" href="111"/>
<i src="images/02.jpg" info="22" href="222"/>
<i src="images/03.jpg" info="33" href="333"/>
<i src="images/04.jpg" info="44" href="444"/>
</images>
主要修改src、info、href的值,求cs代码 谢谢 展开
3个回答
展开全部
string path = Server.MapPath("XML文件虚拟路径");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList noList = doc.SelectNodes("/images/i");
foreach (XmlNode noItem in noList)
{
XmlAttribute src = noItem.Attributes["src"]; //其他属性也可以这么获取和设置,建议做个属性对象不为空的判断
src.InnerText = "设置的值"; //也可以用InnerXml
}
doc.Save(path);
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList noList = doc.SelectNodes("/images/i");
foreach (XmlNode noItem in noList)
{
XmlAttribute src = noItem.Attributes["src"]; //其他属性也可以这么获取和设置,建议做个属性对象不为空的判断
src.InnerText = "设置的值"; //也可以用InnerXml
}
doc.Save(path);
展开全部
using System.Xml;
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNode scr, href, info;
//修改第一行的值
href = doc.SelectSingleNode("/images/i[1]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[1]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[1]/@info");
info.Value = "new value";
//修改第二行的值
href = doc.SelectSingleNode("/images/i[2]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[2]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[2]/@info");
info.Value = "new value";
doc.Save("XMLFile1.xml");
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNode scr, href, info;
//修改第一行的值
href = doc.SelectSingleNode("/images/i[1]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[1]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[1]/@info");
info.Value = "new value";
//修改第二行的值
href = doc.SelectSingleNode("/images/i[2]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[2]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[2]/@info");
info.Value = "new value";
doc.Save("XMLFile1.xml");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System.Xml;
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNode scr, href, info;
//修改第一行的值
href = doc.SelectSingleNode("/images/i[1]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[1]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[1]/@info");
info.Value = "new value";
//修改第二行的值
href = doc.SelectSingleNode("/images/i[2]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[2]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[2]/@info");
info.Value = "new value";
doc.Save("XMLFile1.xml");
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlNode scr, href, info;
//修改第一行的值
href = doc.SelectSingleNode("/images/i[1]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[1]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[1]/@info");
info.Value = "new value";
//修改第二行的值
href = doc.SelectSingleNode("/images/i[2]/@href");
href.Value = "new href";
scr = doc.SelectSingleNode("/images/i[2]/@src");
scr.Value = "new scr";
info = doc.SelectSingleNode("/images/i[2]/@info");
info.Value = "new value";
doc.Save("XMLFile1.xml");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询