110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//读取XML
//已知属性名
//XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.Load(Server.MapPath("test.xml"));
//XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
//XmlNodeList xnl = xn.ChildNodes;
//foreach (XmlNode xn1 in xnl)
//{
// XmlElement xe = (XmlElement)xn1;
// Response.Write(xe.GetAttribute("genre")+"<BR>");
// Response.Write(xe.GetAttribute("ISBN") + "<BR>");
// XmlNodeList subxn = xe.ChildNodes;
// foreach (XmlNode xn2 in subxn)
// {
// Response.Write(xn2.InnerText+"<BR>");
// }
// Response.Write("<BR>");
//}
//未知属性名
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNode xn = XmlDoc.SelectSingleNode("bookstore");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
XmlElement xe = (XmlElement)xn1;
XmlAttributeCollection xc = xe.Attributes;
for (int i = 0; i < xe.Attributes.Count; i++)
{
Response.Write(xc.Item(i).Name + ": ");
Response.Write(xc.Item(i).Value + "<BR>");
}
XmlNodeList xnl2 = xe.ChildNodes;
foreach (XmlNode xn2 in xnl2)
{
Response.Write(xn2.InnerText);
Response.Write("<BR>");
}
Response.Write("<BR>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//写入XML
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNode xn = XmlDoc.SelectSingleNode("bookstore");
XmlElement subxml1 = XmlDoc.CreateElement("book");
subxml1.SetAttribute("genre", "leo");
subxml1.SetAttribute("ISBN", "123-456-789");
XmlElement subXmlxe1 = XmlDoc.CreateElement("title");
subXmlxe1.InnerText = "饮食学";
subxml1.AppendChild(subXmlxe1);
XmlElement subXmlxe2 = XmlDoc.CreateElement("author");
subXmlxe2.InnerText = "秦浩";
subxml1.AppendChild(subXmlxe2);
XmlElement subXmlxe3 = XmlDoc.CreateElement("price");
subXmlxe3.InnerText = "888888";
subxml1.AppendChild(subXmlxe3);
xn.AppendChild(subxml1);
XmlDoc.Save(Server.MapPath("test.xml"));
}
protected void Button3_Click(object sender, EventArgs e)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNodeList xnl = XmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if(xe.GetAttribute("genre")=="leo")
{
xe.SetAttribute("genre","qinhao");
XmlNodeList xel = xe.ChildNodes;
foreach(XmlNode xn1 in xel)
{
XmlElement xe1=(XmlElement)xn1;
if(xe1.Name=="title")
{
xe1.InnerText = "C#";
}
}
}
}
XmlDoc.Save(Server.MapPath("test.xml"));
}
protected void Button4_Click(object sender, EventArgs e)
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(Server.MapPath("test.xml"));
XmlNodeList xnl1 = XmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode xn1 in xnl1 )
{
XmlElement xe = (XmlElement)xn1;
if (xe.GetAttribute("genre") == "qinhao")
{
//xe.RemoveAttribute("genre");
xe.RemoveAll();
}
}
XmlDoc.Save(Server.MapPath("test.xml"));
}
}