生成xml(两种方法)


方法一: 使用Dataset,将数据直接生成到xml中
        SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=sa;Database=northwind;Persist Security Info=True";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from 表", conn);
SqlCommandBuilder thisBulder = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml(@"D:\temp.xml");


方法二: 自定义XML,使用XmlDocument
using System.Xml;//引用命名空间

XmlDocument xml = new XmlDocument();//创建XML文档
XmlDeclaration xNode = xml.CreateXmlDeclaration("1.0", "GBK", null);//参数的第二项为编码方式
xml.AppendChild(xNode);


XmlElement xElement = xml.CreateElement("Root");//创建一个Root根元素
xml.AppendChild(xe);//Root根元素创建完成
XmlNode root = xml.SelectSingleNode("Root");//查找<Root>
XmlElement xe1 = xml.CreateElement("Tree");//在<Root>之下创建元素<Tree>
xe1.SetAttribute("id","1");//指定属性的属性值
xe1.InnerText = "内容";//指定属性文本节点
root.AppendChild(xe1);//完成子节点<Tree>
xml.Save(Server.MapPath("Tempt.xml"));

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值