ASP.NET 创建XML

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Select();
    }
    private void Select()
    {
        string sqlcon = ConfigurationManager.ConnectionStrings["con"].ToString();
        SqlConnection conn = new SqlConnection(sqlcon);
        conn.Open();
        string sql = "select top 3 * from 抽奖结果 order by newid()";
        string sql2="";
        SqlDataAdapter ap = new SqlDataAdapter(sql, conn);
        DataTable ds=new DataTable ();
        ap.Fill(ds);
        conn.Close();
        //创建xml
        XmlDocument xml = new XmlDocument();
        XmlElement element = xml.CreateElement("xml");
        element.SetAttribute("Version", "2.0");//添加属性
        element.SetAttribute("encoding", "utf-8");
        element.SetAttribute("xmlns:media", "
http://search.yahoo.com/mrss/");
        for (int i = 0; i < ds.Rows.Count; i++)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Open();
            }
            else if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            string sql3 = "select count(*) from dbo.得奖用户 where code=" + ds.Rows[i][0];//如果当前的奖项已经被抽中,则不会在被抽取
            SqlCommand an = new SqlCommand(sql3,conn);
            if (Convert.ToInt32(an.ExecuteScalar()) <= 0)
            {
                sql2 += "insert into 得奖用户 values ('" + ds.Rows[i][0].ToString().Trim() + "');";
                XmlElement node = xml.CreateElement("tt" + ds.Rows[i][0].ToString());
                node.SetAttribute("name", "txt" + ds.Rows[i][0].ToString());
                node.InnerText = ds.Rows[i][0].ToString();
                element.AppendChild(node);
            }
        }
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Open();
        }
        else if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        SqlCommand command = new SqlCommand(sql2, conn);
        command.ExecuteNonQuery();//将当前的抽奖结果记录下来
        xml.AppendChild(element);
        xml.Save(Server.MapPath("~/code.xml"));
        Response.Redirect("~/code.xml");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值