(新手)如何读取XML文件中的节点值,并修改,在线等!

sqlaspnet55 2010-05-05 09:19:38
新手上路,请多指教。

我想在程序中 ,通过dropdownlist选中要修改的节点,然后在textbox中就能显示出它的值,重新输入一个值,再点修改按钮,就能改的那种。

不要只说方法 方法网上有很多 我要具体的代码,最好标注一下,多谢了。

急!

xml文件如下(就是广告):
<Advertisements>
<Ad>
<ImageUrl>images/admin_image/sohu.jpg</ImageUrl>
<NavigateUrl>http://www.sohu.com</NavigateUrl>
<AlternateText>bbb</AlternateText>
<Keyword>1</Keyword>
<Impressions>100</Impressions>
</Ad>
</Advertisements>
...全文
427 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdlpc 2010-05-06
  • 打赏
  • 举报
回复
本人不会,来学习学习...
z195389542 2010-05-06
  • 打赏
  • 举报
回复
帮顶!~~~~~~~
辰爸 2010-05-06
  • 打赏
  • 举报
回复
学习,标记下!~
sqlaspnet55 2010-05-06
  • 打赏
  • 举报
回复
解决了,多谢各位。

散分。
  • 打赏
  • 举报
回复
测试过了.注意ImageUrl节点中不要存在重复的值.

<asp:DropDownList AutoPostBack="true" AppendDataBoundItems="true" ID="adlist" runat="server"
onselectedindexchanged="adlist_SelectedIndexChanged" >
<asp:ListItem Value="">请选择</asp:ListItem>
</asp:DropDownList>

ImageUrl <asp:TextBox ID="txtNavigateUrl" runat="server" />
<asp:Button ID="btn" runat="server" Text="OK" onclick="btn_Click"
style="height: 26px" />

<asp:AdRotator ID="AdRotator1" AdvertisementFile="~/ad.xml" runat="server" />




public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/ad.xml"));

System.Xml.XmlNodeList list = doc.SelectNodes("/Advertisements/Ad/ImageUrl");
foreach (System.Xml.XmlNode node in list)
{
adlist.Items.Add(node.InnerText);
}

}
}

protected void adlist_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = adlist.SelectedValue;

if (!string.IsNullOrEmpty(selectedValue))
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/ad.xml"));
System.Xml.XmlNode n = doc.SelectSingleNode(string.Format("/Advertisements/Ad[ImageUrl='{0}']", selectedValue));
if (n != null)
{
txtNavigateUrl.Text = n.ChildNodes[3].InnerText;
}
}
}

protected void btn_Click(object sender, EventArgs e)
{
string strNavigateUrl = txtNavigateUrl.Text.Trim();
if (!string.IsNullOrEmpty(strNavigateUrl))
{
string selectedValue = adlist.SelectedValue;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(HttpContext.Current.Server.MapPath("~/ad.xml"));
System.Xml.XmlNode n = doc.SelectSingleNode(string.Format("/Advertisements/Ad[ImageUrl='{0}']", selectedValue));
if (n != null)
{
n.ChildNodes[3].InnerText = strNavigateUrl;
doc.Save(HttpContext.Current.Server.MapPath("~/ad.xml"));
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "succeeded", "alert('成功');", true);
}
}
}
}



<?xml version="1.0" encoding="utf-8"?>
<Advertisements>
<Ad>
<ImageUrl>~/Images/image1.jpg</ImageUrl>
<height>60</height>
<width>190</width>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Microsoft Main Site</AlternateText>
<Impressions>80</Impressions>
<Keyword>Topic1</Keyword>
</Ad>
<Ad>
<ImageUrl>~/Images/image2.jpg</ImageUrl>
<height>90</height>
<width>90</width>
<NavigateUrl>http://www.wingtiptoys.com</NavigateUrl>
<AlternateText>Wingtip Toys</AlternateText>
<Impressions>80</Impressions>
<Keyword>Topic2</Keyword>
</Ad>
</Advertisements>
wuyq11 2010-05-05
  • 打赏
  • 举报
回复
foreach(XmlElement e in xmldoc.DocumentElement.SelectNodes(""))
{
e.Attributes[""].Value =""
}
xmldoc.Save("");

xml
xray2005 2010-05-05
  • 打赏
  • 举报
回复

先会走,才能跑。。。 一步步的来,并不是说程序出来就完了,自己写出来才算会
xrongzhen 2010-05-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sqlaspnet55 的回复:]
我想用dropdownlist来选择要修改的节点,再把值加载到textbox上,得怎么做呀?
[/Quote]

不就是点击触发事件 在事件中处理吗? 上面已经给你操作xml的事例了,改一下不就好了。

先会走,才能跑。。。 一步步的来,并不是说程序出来就完了,自己写出来才算会
sqlaspnet55 2010-05-05
  • 打赏
  • 举报
回复
我想用dropdownlist来选择要修改的节点,再把值加载到textbox上,得怎么做呀?
wuyq11 2010-05-05
  • 打赏
  • 举报
回复
XmlDocument xml = new XmlDocument();
xml.Load(@"");
XmlNode xl = xml.SelectSingleNode("Ad/ImageUrl");
if (xl != null)
{
Console.WriteLine(xl.InnerText);

}

修改一下昵称 2010-05-05
  • 打赏
  • 举报
回复
给你一个对xml(增删改查)的例子
你对照自己的项目改改,应该很简单


using System;
using System.Data;
using System.Configuration;
using System.Collections;

using System.Xml;

public class Util_Xml
{
string xmlFile = System.Configuration.ConfigurationManager.AppSettings["xmlFile"];
XmlDocument XmlDoc = new XmlDocument();

//添加
protected void XmlAdd()
{
XmlNode objRootNode = XmlDoc.SelectSingleNode("//Root"); //声明XmlNode对象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //创建XmlElement对象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???结点和元素的区别?方法都一样.
objElement.InnerText = "tree1";
objChildNode.AppendChild(objElement);
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}

//删除
protected void XmlDelete()
{
string Node = "//Root/Student[Name='tree1']";//Xml是严格区分大小写的.
XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}

//更新
protected void XmlUpdate()
{
//XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']/Name").InnerText = "tree2";
XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']").Attributes["id"].Value = "001";
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}

//查询
protected void XmlQuery()
{
XmlNodeList NodeList = XmlDoc.SelectNodes("//Root/Student");//查询全部student节点
//循环遍历节点,查询是否存在该节点
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}

//查询单个节点,//表示全部匹配的元素./表示以此为根的子元素.javascript下的查询也是一样.
string XmlPathNode = "//Root/Student[Name='rock']/Photo";
Response.Write(XmlDoc.SelectSingleNode(XmlPathNode).InnerText);
}
}





<?xml version="1.0" encoding="gb2312"?>
<Root>
<Student Admin="no">
<Name>rock</Name>
<NickName>rock1</NickName>
<Pwd>123</Pwd>
<Sex>男生</Sex>
<Birthday>1986-1-1</Birthday>
<Email>xymac@163.com</Email>
<QQ>123374355</QQ>
<Msn>loveplc@live.cn</Msn>
<Tel>13005129336</Tel>
<Homepage>http://www.loveplc.cn</Homepage>
<Address>广州</Address>
<Work>asp.net菜鸟</Work>
<Photo>images/rock.gif</Photo>
<Time>2008-3-18 10:15:29</Time>
</Student>
<Student Admin="yes">
<Name>tree</Name>
<NickName>宿舍老大</NickName>
<Pwd>51aspx</Pwd>
<Sex>男生</Sex>
<Birthday>
</Birthday>
<Email>support@tree.com</Email>
<QQ>
</QQ>
<Msn>
</Msn>
<Tel>
</Tel>
<Homepage>
</Homepage>
<Address>
</Address>
<Work>
</Work>
<Photo>
</Photo>
<Time>2008-3-26 11:39:57</Time>
</Student>
<Student>
<Name>tree2</Name>
</Student>
<Student id="001">
<Name>tree1</Name>
</Student>
</Root>

mengxj85 2010-05-05
  • 打赏
  • 举报
回复

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧