asp.net生成.xml文件的两种方法
2015-02-12 10:08
337 查看
方式一:直接使用DataSet
[c-sharp] view
plaincopy
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(@"C:/temp.xml");
方式二:自定义生成方式
[c-sharp] view
plaincopy
using System.Xml;//头部加此命名空间
XmlDocument xd = new XmlDocument();//表示XML文档
XmlDeclaration xde;//表示 XML 声明节点:<?xml version='1.0'...?>
xde = xd.CreateXmlDeclaration("1.0", "GBK", null);//参数的第二项为编码方式
//standalone定义了是否可以在不读取任何其它文件的情况下处理该文档,默认为no
xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束
XmlElement xe = xd.CreateElement("Root");//创建一个Root根元素
xd.AppendChild(xe);//Root根元素创建完成
XmlNode root = xd.SelectSingleNode("Root");//查找<Root>
XmlElement xe1 = xd.CreateElement("Tree");//在<Root>之下创建元素<Tree>
xe1.SetAttribute("id","1");//指定属性的属性值
xe1.InnerText = "类型1";//指定属性文本节点
root.AppendChild(xe1);//完成子节点<Tree>
xd.Save(Server.MapPath("xml.xml"));
[c-sharp] view
plaincopy
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(@"C:/temp.xml");
方式二:自定义生成方式
[c-sharp] view
plaincopy
using System.Xml;//头部加此命名空间
XmlDocument xd = new XmlDocument();//表示XML文档
XmlDeclaration xde;//表示 XML 声明节点:<?xml version='1.0'...?>
xde = xd.CreateXmlDeclaration("1.0", "GBK", null);//参数的第二项为编码方式
//standalone定义了是否可以在不读取任何其它文件的情况下处理该文档,默认为no
xd.AppendChild(xde);//<?xml version="1.0" encoding="UTF-8" standalone="yes"?>生成结束
XmlElement xe = xd.CreateElement("Root");//创建一个Root根元素
xd.AppendChild(xe);//Root根元素创建完成
XmlNode root = xd.SelectSingleNode("Root");//查找<Root>
XmlElement xe1 = xd.CreateElement("Tree");//在<Root>之下创建元素<Tree>
xe1.SetAttribute("id","1");//指定属性的属性值
xe1.InnerText = "类型1";//指定属性文本节点
root.AppendChild(xe1);//完成子节点<Tree>
xd.Save(Server.MapPath("xml.xml"));
相关文章推荐
- asp.net简单生成XML文件的方法
- 在ASP.NET页面中显示自动生成图片的两种方法
- asp.net处理XML文件的两种方法
- asp.net生成静态页的两种方法
- Asp.net在权限驱动下动态生成菜单的两种方法
- ASP.NET中自动生成XML文件并通过XSLT显示在网页中的方法
- Flex :HTTPService处理XML文件的两种方法, Asp生成Xml的方法,以及Flex定义变量、赋值的运用。。。。
- asp.net生成静态页的两种方法
- asp.net生成静态页的两种方法成技术
- 用asp.net生成html静态页的两种方法~~~
- 第三日:asp.net生成xml文件的方法
- asp.net简单生成XML文件的方法
- ASP.NET生成静态网页的方法
- 在ASP.Net中两种利用CSS实现多界面的方法
- 在ASP.Net中两种利用CSS实现多界面的方法
- Asp.net生成静态页面的方法
- ASP.NET生成HTML文件的方法
- ASP.NET生成静态网页的方法
- ASP信息页面自动生成HTML的两种方法
- 在ASP.Net中两种利用CSS实现多界面的方法