XML的序列化用法 vs平台开发
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
NewMethod();
}
}
#region 读取数据
protected void Button3_Click(object sender, EventArgs e)
{
//获取文档地址
string filename = Server.MapPath("info.xls");
DataTable fuck = showinfo(filename);
if (fuck == null)
{
Response.Write("<script>alert('读取失败')</script>");
}
else
{
Response.Write("<script>alert('读取成功')</script>");
}
}
#endregion
#region 读取数据方法
public static DataTable showinfo(string filename)
{
//转换格式
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + ";" + "Extended Properties=Excel 8.0;";
//创建连接
OleDbConnection con = new OleDbConnection(strcon);
con.Open();
//查询
string str = "select * from [info$]";
//读取数据到数据集
OleDbDataAdapter dap = new OleDbDataAdapter(str, con);
//实例化数据集
DataSet dt = new DataSet();
//添加
dap.Fill(dt);
con.Close();
return dt.Tables[0];
}
#endregion
#region 添加到XML数据里
protected void Button4_Click(object sender, EventArgs e)
{
DataTable dt = showinfo(Server.MapPath("info.xls"));
dt.WriteXml(Server.MapPath("info.xml"));
}
#endregion
#region 显示xml里的数据
private void NewMethod()
{
//将xml数据更新到dataset里
DataTable dt = new DataTable();
dt.Columns.Add("sid");
dt.Columns.Add("sname");
dt.Columns.Add("sage");
//源 --xml 加载
XDocument doc1 = XDocument.Load(Server.MapPath("info.xml"));
//查找根yuansu
XElement root1 = doc1.Root;
//查找根下的所有table节
IEnumerable<XElement> list1 = root1.Elements();
//循环table下的数据
foreach (XElement item in list1)
{
//行数据
DataRow dr = dt.NewRow();
foreach (var i in item.Elements())
{
dr[i.Name.ToString()] = i.Value; //把列的内容写入行
}
dt.Rows.Add(dr); //把行的内容写入表
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
#endregion
#region 分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
NewMethod();
}
#endregion
#region 添加数据
protected void Button5_Click(object sender, EventArgs e)
{
//加载xml
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("info.xml"));
//找个根结构
XmlNode root = doc.SelectSingleNode("NewDataSet");
//创建子节点
XmlElement subnode = doc.CreateElement("Table");
//在当前子节点下面创建元素节点
XmlNode node1 = doc.CreateNode(XmlNodeType.Element, "sid", null);
node1.InnerText = TextBox1.Text;
subnode.AppendChild(node1);
XmlNode node2 = doc.CreateNode(XmlNodeType.Element, "sname", null);
node2.InnerText = TextBox2.Text;
subnode.AppendChild(node2);
XmlNode node3 = doc.CreateNode(XmlNodeType.Element, "sage", null);
node3.InnerText = TextBox3.Text;
subnode.AppendChild(node3);
root.AppendChild(subnode);
//保存xml文档
doc.Save(Server.MapPath("info.xml"));
NewMethod();
}
#endregion
#region 删除 修改
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="del")
{
string sid = e.CommandArgument.ToString();
XDocument doc1 = XDocument.Load(Server.MapPath("info.xml"));
XElement root1 = doc1.Root;
IEnumerable<XElement> list1 = root1.Elements();
foreach (XElement item in list1)
{
foreach (XElement i in item.Elements())
{
if(i.Name=="sid" && i.Value==sid)
{
item.Remove();
Response.Write("<script>alert('删除成功')</script>");
doc1.Save(Server.MapPath("info.xml"));
NewMethod();
return;
}
}
}
}
if(e.CommandName=="update")
{
Session["sid"] = e.CommandArgument;
Response.Redirect("Update.aspx");
}
}
#endregion