您的位置:首页 > 编程语言 > C#

c#动态生成xml文件

2013-05-25 19:35 423 查看
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

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: