//添加节点
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(" <所有图书>" + " <图书>" + " <书名>ASP.NET程序开发范例宝典 </书名>" + " </图书>" + " </所有图书>");
XmlDocument doc2 = new XmlDocument();
doc2.Load(Server.MapPath("book.xml"));
XmlNode newNode = doc1.ImportNode(doc2.DocumentElement.LastChild,true);
doc1.DocumentElement.AppendChild(newNode);
doc1.Save(Server.MapPath("insert.xml"));
Xml1.Document = doc1;
//xml修改
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet mydsRead = new DataSet();
mydsRead.ReadXml(Server.MapPath("Test.xml"));
GridView1.DataSource = mydsRead;
GridView1.DataBind();
DropDownList1.DataSource = mydsRead;
DropDownList1.DataTextField = "EName";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Test.xml"));
XmlNodeList xnl = doc.SelectSingleNode("NewDataSet").ChildNodes; ;//获取NewDataSet节点的所有子节点
foreach (XmlNode xn in xnl)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.Name == "Table")//判断节点名为Table
{
XmlNodeList xnlChild = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xnChild in xnlChild)//遍历
{
XmlElement xeChild = (XmlElement)xnChild;//转换类型
if (xeChild.Name == "EName" && xeChild.InnerText == this.DropDownList1.SelectedValue.Trim())
{
xeChild.InnerText = TextBox1.Text.Trim();
Response.Write(" <script>alert('修改成功') </script>");
}
}
}
}
doc.Save(Server.MapPath("Test.xml"));
Response.Write(" <script>location='Default.aspx' </script>");
}
//检索xml节点
protected void Page_Load(object sender, EventArgs e)
{
string strCon="Data Source=(local);Database=db_15;Uid=sa;Pwd=";
SqlConnection sqlcon = new SqlConnection(strCon);
string sqlstr = "select * from tb_Employee";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr,sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds);
sqlcon.Close();
myds.WriteXml(Server.MapPath("test.xml"));
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes("descendant::Table[EName='" + TextBox1.Text.Trim() + "']");
foreach (XmlNode node in nodes)
{
if (Label1.Text == "")
{
for (int i = 0; i <= node.ChildNodes.Count - 1; i++)
{
Label1.Text = Label1.Text + node.ChildNodes[i].InnerText + " <br>";
}
}
else
{
Label1.Text = "";
for (int i = 0; i <= node.ChildNodes.Count - 1; i++)
{
Label1.Text = Label1.Text + node.ChildNodes[i].InnerText + " <br>";
}
}
}
}
//删除xml节点
if (TextBox1.Text.Trim() != "")
{
Label1.Visible = true;
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes("descendant::Table[EName='" + TextBox1.Text.Trim() + "']");
foreach (XmlNode node in nodes)
{
root.RemoveChild(node);
}
doc.Save(Server.MapPath("test.xml"));
Xml2.Document = doc;
}
else
Response.Write(" <script>alert('请输入要删除的节点');location='javascript:history.go(-1)';
XML保存数据
最新推荐文章于 2024-08-08 10:02:44 发布