asp.net数据库生成xml文件

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

/// <summary>
///data2xml 的摘要说明
/// </summary>
public class data2xml
{
	public data2xml()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//

	}

    /// <summary>
    /// 提取top10的题目生成question.xml
    /// </summary>
    public void qlist()
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration declareation;
        declareation = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xmlDoc.AppendChild(declareation);

        XmlElement xeRoot = xmlDoc.CreateElement("content");
        xmlDoc.AppendChild(xeRoot);

        XmlNode root = xmlDoc.SelectSingleNode("content");
        

        AccessDbClass newDB = new AccessDbClass();
        OleDbDataAdapter da = newDB.SelectToOleDbDataAdapter("select top 10 * from [question] where [qState]=1 order by rnd([qID])");
        DataSet ds = new DataSet();
        da.Fill(ds);
        newDB.Close();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            XmlElement xeQuestion = xmlDoc.CreateElement("question");

                XmlElement xeTitle = xmlDoc.CreateElement("title");
                xeTitle.InnerText = ds.Tables[0].Rows[i]["qTitle"].ToString();
                xeQuestion.AppendChild(xeTitle);

                XmlElement xeID = xmlDoc.CreateElement("ID");
                xeID.InnerText = ds.Tables[0].Rows[i]["qID"].ToString();
                xeQuestion.AppendChild(xeID);

                XmlElement xeAnswer = xmlDoc.CreateElement("answer");
                xeAnswer.SetAttribute("type", ds.Tables[0].Rows[i]["qType"].ToString());

                    string qAnswer = ds.Tables[0].Rows[i]["qAnswer"].ToString();
                    string[] q_Answer = qAnswer.Split('|');

                    string qVote = ds.Tables[0].Rows[i]["qVote"].ToString();
                    string[] q_Vote = qVote.Split(',');

                    for (int j = 0; j < q_Answer.Length; j++)
                    {
                        XmlElement xeLabel = xmlDoc.CreateElement("label");
                        xeLabel.SetAttribute("vote", q_Vote[j].ToString());
                        xeLabel.SetAttribute("id", (j+1).ToString());
                        xeLabel.InnerText = q_Answer[j].ToString();
                        xeAnswer.AppendChild(xeLabel);
                    }

               xeQuestion.AppendChild(xeAnswer);

            xeRoot.AppendChild(xeQuestion);
        }
        

        xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "\\question.xml");

    }



    /// <summary>
    /// 获取相应留言ID生成id.xml
    /// </summary>
    /// <param name="num">留言ID</param>
    public void mlist(int num)
    {
        //<?xml version="1.0" encoding="UTF-8"?>
        //<leaveWord>
	        //<content><![CDATA[中山狼,得志更猖狂.]]></content>
        //</leaveWord>


        //表示XML文档
        XmlDocument xmlDoc = new XmlDocument();

        //表示 XML 声明节点
        //standalone定义了是否可以在不读取任何其它文件的情况下处理该文档,默认为no
        //<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束
        XmlDeclaration declareation;
        declareation = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xmlDoc.AppendChild(declareation);

        //创建一个leaveWord根元素
        XmlElement xeRoot = xmlDoc.CreateElement("leaveWord");
        xmlDoc.AppendChild(xeRoot);//Root根元素创建完成

        //查找<leaveWord>
        XmlNode root = xmlDoc.SelectSingleNode("leaveWord");

        AccessDbClass newDB = new AccessDbClass();
        OleDbDataAdapter da = newDB.SelectToOleDbDataAdapter("select * from [message] where [qid]=" + num +" order by [addTime] desc");
        DataSet ds = new DataSet();
        da.Fill(ds);
        newDB.Close();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            //在<leaveWord>之下创建元素<content>
            //XmlElement xeContent = xmlDoc.CreateElement("content");
            //指定属性的属性值
            //xeContent.SetAttribute("id", "1");
            //完成子节点<content>
            //root.AppendChild(xeContent);

            XmlCDataSection xeContent = xmlDoc.CreateCDataSection("content");
            xeContent.InnerText = ds.Tables[0].Rows[i]["mContent"].ToString();
            xeRoot.AppendChild(xeContent);
        }
        xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +"\\"+num+".xml");
    }
}

  

posted on 2013-04-11 17:47  douqiumiao  阅读(297)  评论(0编辑  收藏  举报