asp.net简单生成XML文件的方法
2016-08-09来源:

本文实例讲述了asp.net简单生成XML文件的方法。分享给大家供大家参考,具体如下:

方式一:直接使用DataSet

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");

方式二:自定义生成方式

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程序设计有所帮助。

2025公考·省考培训课程试听预约报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
推荐信息
Baidu
map