Code
//xml写入
public partial class XML应用_XML写入 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet("myDs");
//从XML中读取数据
ds.ReadXml(Server.MapPath(@".\db\Guest.xml"));
//以下为添加记录
DataRow dr = ds.Tables[0].NewRow();
dr["Name"] = tbName.Text.Trim();
dr["City"] = tbCity.Text.Trim();
dr["Email"] = tbEmail.Text.Trim();
dr["Message"] = tbComments.Text.Trim();
dr["STime"] = DateTime.Now.ToString();
ds.Tables[0].Rows.Add(dr);
//写回XML
ds.WriteXml(Server.MapPath(".\\db\\Guest.xml"));
Response.Redirect("view.aspx");
}
}
//xml读取操
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
public partial class XML应用_XML读取 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
//显示在datagird上
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));//取当前路径下DB文件夹下的的dbGuest.xml文件
//ds.ReadXml(Server.MapPath("db/XMLFile.xml"));//取当前路径下DB文件夹下的的XMLFile.xml文件
//ds.ReadXml(Server.MapPath("XMLFile.xml")); //取当前路径(文件夹)下dbGuest.xml文件
//ds.ReadXml(Server.MapPath("../OpXMLFile/XMLFile.xml"));//取当前路径(文件夹)外的dbGuest.xml文件
//ds.ReadXml(Server.MapPath("../XMLBU/DB/XMLFile.xml"));//取不是当前路径下而是XMLBU/DB下的XMLFile.xml
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();//datagrid
//把姓名绑定在dropdownlist
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Name");
ddlName.Items.Clear();
for (int i = 0; i < elemList.Count; i++)
ddlName.Items.Add(elemList[i].InnerXml);//获取或者设置代表该节点的字节点的标记
}
protected void btnQuery_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
lbEmail.Text = doc.SelectSingleNode("//User[Name='" + ddlName.SelectedItem.Text + "']").ChildNodes.Item(2).InnerText;
}
protected void btnChange_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
{
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "Email")//如果找到
{
xe2.InnerText = tbNewMail.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
protected void btnDelete_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
protected void btnAdd_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNode root = xmlDoc.SelectSingleNode("dbGuest");//查找<dbGuest>
XmlElement xe1 = xmlDoc.CreateElement("User");//创建一个<User>节点
XmlElement xesub1 = xmlDoc.CreateElement("Name");
xesub1.InnerText = "Guset";//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2 = xmlDoc.CreateElement("City");
xesub2.InnerText = "上海";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("Email");
xesub3.InnerText = "ss@22.net";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
}
//xml写入
public partial class XML应用_XML写入 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet("myDs");
//从XML中读取数据
ds.ReadXml(Server.MapPath(@".\db\Guest.xml"));
//以下为添加记录
DataRow dr = ds.Tables[0].NewRow();
dr["Name"] = tbName.Text.Trim();
dr["City"] = tbCity.Text.Trim();
dr["Email"] = tbEmail.Text.Trim();
dr["Message"] = tbComments.Text.Trim();
dr["STime"] = DateTime.Now.ToString();
ds.Tables[0].Rows.Add(dr);
//写回XML
ds.WriteXml(Server.MapPath(".\\db\\Guest.xml"));
Response.Redirect("view.aspx");
}
}
//xml读取操
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
public partial class XML应用_XML读取 : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
//显示在datagird上
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));//取当前路径下DB文件夹下的的dbGuest.xml文件
//ds.ReadXml(Server.MapPath("db/XMLFile.xml"));//取当前路径下DB文件夹下的的XMLFile.xml文件
//ds.ReadXml(Server.MapPath("XMLFile.xml")); //取当前路径(文件夹)下dbGuest.xml文件
//ds.ReadXml(Server.MapPath("../OpXMLFile/XMLFile.xml"));//取当前路径(文件夹)外的dbGuest.xml文件
//ds.ReadXml(Server.MapPath("../XMLBU/DB/XMLFile.xml"));//取不是当前路径下而是XMLBU/DB下的XMLFile.xml
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();//datagrid
//把姓名绑定在dropdownlist
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("Name");
ddlName.Items.Clear();
for (int i = 0; i < elemList.Count; i++)
ddlName.Items.Add(elemList[i].InnerXml);//获取或者设置代表该节点的字节点的标记
}
protected void btnQuery_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
lbEmail.Text = doc.SelectSingleNode("//User[Name='" + ddlName.SelectedItem.Text + "']").ChildNodes.Item(2).InnerText;
}
protected void btnChange_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
{
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "Email")//如果找到
{
xe2.InnerText = tbNewMail.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
protected void btnDelete_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("Name");
if (node.Count > 0)
{
if (node[0].InnerText == ddlName.SelectedItem.Text)
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
protected void btnAdd_Click(object sender, System.EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
XmlNode root = xmlDoc.SelectSingleNode("dbGuest");//查找<dbGuest>
XmlElement xe1 = xmlDoc.CreateElement("User");//创建一个<User>节点
XmlElement xesub1 = xmlDoc.CreateElement("Name");
xesub1.InnerText = "Guset";//设置文本节点
xe1.AppendChild(xesub1);//添加到<User>节点中
XmlElement xesub2 = xmlDoc.CreateElement("City");
xesub2.InnerText = "上海";
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("Email");
xesub3.InnerText = "ss@22.net";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//添加到<dbGuest>节点中
xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
Bind();
}
}