方法一: 使用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"));

被折叠的 条评论
为什么被折叠?



