c#动态生成xml文件

来源:互联网 发布:怎样将占用的端口释放 编辑:程序博客网 时间:2024/05/17 19:18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NIBM.DAL;
using System.Data;
using System.Xml;


namespace NIBM.SysWeb {
    public partial class form : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            SQLDAL dal = new SQLDAL(); 
            DataSet ds = dal.GetList("*", "TSvr_VPS_VerifyInfo", "VerifySeq=10", "");


            XmlDocument doc = new XmlDocument();
            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
            
            XmlElement root = doc.CreateElement("Apply");
            doc.AppendChild(root);


            foreach (DataRow dr in ds.Tables[0].Rows) {


                for (int i = 0;i<dr.Table.Columns.Count; i++) //列数
                {
                    XmlElement child = doc.CreateElement(dr.Table.Columns[i].ColumnName);
                    XmlAttribute attr = doc.CreateAttribute("value");
                    attr.Value = dr.ItemArray[i].ToString();
                    child.Attributes.Append(attr);
                    root.AppendChild(child);
                }
                string xmlString = doc.OuterXml;
                doc.Save(@"C:\PrintXML.xml"); //保存xml文件,取名为PrintXML.xml


            }
        }
    }
}
原创粉丝点击