XML与DataSet操作使用
2010-01-23 19:37 三皮开发时 阅读(305) 评论(0) 编辑 收藏 举报1.基本
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = GetCon();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from tb_VoteItem";
cmd.Connection = con;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds,"xmlTable");
con.Close();
ds.WriteXml(Server.MapPath("WriteXml.xml"));
Response.Write("<script>alert('写入成功!')</script>");
}
public SqlConnection GetCon()
{
SqlConnection con = new SqlConnection(str);
return con;
}
//xml导入数据控件
protected void Button2_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("WriteXml.xml"));
GridView1.DataSource = ds.Tables["table"];
GridView1.DataBind();
DataList1.DataSource = ds;
DataList1.DataBind();
}
------------------------
2.更新,DropDownList和GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("WriteXml.xml"));
DataList1.DataSource = ds;
DataList1.DataBind();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "ItemName";
DropDownList1.DataBind();
}
}
public SqlConnection GetCon()
{
SqlConnection con = new SqlConnection(str);
return con;
}
//修改
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("WriteXml.xml"));
//获取该xml文件下所有子节点
XmlNodeList xnl = doc.SelectSingleNode("NewDataSet").ChildNodes;
foreach (XmlNode xn in xnl)
{
//将子节点类型转化为XmlElement类型
XmlElement xe = (XmlElement)xn;
if (xe.Name == "xmlTable")
{
XmlNodeList xnlChild = xe.ChildNodes;
foreach (XmlNode xnChild in xnlChild)
{
XmlElement xeChild = (XmlElement)xnChild;
if (xeChild.Name == "ItemName" && xeChild.InnerText == DropDownList1.SelectedValue.Trim())
{
xeChild.InnerText = TextBox1.Text.Trim();
Response.Write("<script>alert('修改成功!')</script>");
}
}
}
}
doc.Save(Server.MapPath("WriteXml.xml"));
Response.Write("<script>location='updateXML.aspx'</script>");
}
-----------------
3.搜索
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(str);
SqlDataAdapter adapt = new SqlDataAdapter("select * from VoteItem",con);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
ds.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[ItemName='"+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 += node.ChildNodes[i].InnerText + "<br/>";
}
}
}
}
label 显示出条件的所有信息
--------------
4.删除
if (TextBox1.Text.Trim() != "")
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Delete.xml"));
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes("descendant::Table[ItemName='"+TextBox1.Text.Trim()+"']");
foreach (XmlNode node in nodes)
{
root.RemoveChild(node);
}
doc.Save(Server.MapPath("Delet.xml"));
Xml1.Document = doc;
Response.Write("<script>alert('删除成功!')</script>");
}
------------------------------------------------------------------------------------------------------------------------
5.添加
//添加
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XmlNode root = doc.SelectSingleNode("NewDataSet");
XmlElement xe1 = doc.CreateElement("Table");
//xe1.SetAttribute("genre", "XXX");//设置属性
//xe1.SetAttribute("ISBN","2-3631-4");
XmlElement xesub1 = doc.CreateElement("voteItemID");
xesub1.InnerText = TextBox1.Text.Trim();
xe1.AppendChild(xesub1);
XmlElement xesub2 = doc.CreateElement("voteID");
xesub2.InnerText = TextBox2.Text.Trim();
xe1.AppendChild(xesub2);
XmlElement xesub3 = doc.CreateElement("voteContent");
xesub3.InnerText = TextBox3.Text.Trim();
xe1.AppendChild(xesub3);
XmlElement xesub4 = doc.CreateElement("voteTotal");
xesub4.InnerText = TextBox4.Text.Trim();
xe1.AppendChild(xesub4);
root.AppendChild(xe1);
doc.Save(Server.MapPath("test.xml"));
Bind();
}