欢迎访问我的博客 https://javascript.shop

net8:XML的读写操作【广告控件的XML文件实例】

原文发布时间为:2008-08-05 —— 来源于本人的百度文章 [由搬家工具导入]

【用了datalist控件,datalist控件自己学会,主要知道其他按钮COMMANDNAME属性应该改为edit,update,delete】

using System;
using System.Data;
using System.Configuration;
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;
using System.Xml.XPath;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
        }

    }

    protected void fill()
    {
        DataSet ds=new DataSet();
        ds.ReadXml(Server.MapPath("~/App_Data/ad.xml"));
        DataList1.DataSource=ds.Tables[0].DefaultView;
        DataList1.DataBind();

        Session["dst"]=ds;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        fill();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        ds.Tables[0].Rows[e.Item.ItemIndex]["ImageUrl"] = ((TextBox)e.Item.FindControl("TextBox2")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["NavigateUrl"] = ((TextBox)e.Item.FindControl("TextBox5")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["AlternateText"] = ((TextBox)e.Item.FindControl("TextBox1")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["Keyword"] = ((TextBox)e.Item.FindControl("TextBox3")).Text.ToString();
        ds.Tables[0].Rows[e.Item.ItemIndex]["Impressions"] = ((TextBox)e.Item.FindControl("TextBox4")).Text.ToString();

        Session["dst"] = ds;
       ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));

        DataList1.EditItemIndex=-1;
        fill();
    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = -1;
        fill();
    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        ds.Tables[0].Rows.Remove(ds.Tables[0].Rows[e.Item.ItemIndex]);
        Session["dst"] = ds;
        ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));

        fill();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        DataSet ds = (DataSet)Session["dst"];
        DataRow dr = ds.Tables[0].NewRow();

        dr["ImageUrl"] = url.Text.ToString();
        dr["NavigateUrl"] = ng.Text.ToString();
        dr["AlternateText"] = na.Text.ToString();
        dr["Keyword"] = kw.Text.ToString();
        dr["Impressions"] = qz.Text.ToString();

        ds.Tables[0].Rows.Add(dr);
        Session["dst"] = ds;

        ds.WriteXml(Server.MapPath("~/App_Data/ad.xml"));
        fill();
        DIV1.Visible = false;
        Panel1.Visible = false;
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        na.Focus();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        DIV1.Visible = true;
        ng.Focus();
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        string name = fu.FileName.ToString();
        //int size = Convert.ToInt32(fu.PostedFile.ContentLength);
        string type1 = fu.PostedFile.ContentType.ToString();
        string type = type1.Substring(0, 5);

        if (type == "image")
        {
            fu.PostedFile.SaveAs(Server.MapPath("~/img/") + name);
            img.ImageUrl = Server.MapPath("~/img/") + name;
            url.Text = Server.MapPath("~/img/") + name;
        }
        else
        {
            url.Text = "不是允许的类型";
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx");
    }
}

----------------------------------------------

ad.xml的内容:

<?xml version="1.0" standalone="yes"?>
<Advertisements>
<Ad>
    <ImageUrl>~/img/logo.gif</ImageUrl>
    <NavigateUrl>http://www.hc115.com</NavigateUrl>
    <AlternateText>洪川医药</AlternateText>
    <Keyword>医院评价</Keyword>
    <Impressions>50</Impressions>
</Ad>
<Ad>
    <ImageUrl>http://www.baidu.com/img/baidu_logo_rw_0808_hzl.gif</ImageUrl>
    <NavigateUrl>http://www.baidu.com</NavigateUrl>
    <AlternateText>百度</AlternateText>
    <Keyword>百度</Keyword>
    <Impressions>60</Impressions>
</Ad>
<Ad>
    <ImageUrl>F:\wuwenjie\net8\img\iask.gif</ImageUrl>
    <NavigateUrl>http://www.iask.com</NavigateUrl>
    <AlternateText>爱问</AlternateText>
    <Keyword>爱问搜</Keyword>
    <Impressions>30</Impressions>
</Ad>
</Advertisements>

posted @ 2017-07-09 15:04  孑孓子  阅读(125)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop