62,053
社区成员
发帖
与我相关
我的任务
分享
<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>
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>