ASP.net Xml: DataSet的ReadXml(), WriteXml()和Response写Xml文档

 专题图ylbtech-asp.net-logo编号:ylbtechASPnetXml100010011

1,功能描述

   这是一个基于.net操作Xml的案例示例,共有两个示例。

ylb_menu:1, DemoReadXml (DataSet的ReadXml())

ylb_menu:2, DemoWriteXml(DataSet的WriteXml()和Response写Xml文档)

 

2,技术与环境

操作系统:

windows

开发语言:

C#

开发框架:

 

数据库:

开发软件:

Microsoft Visual Studio 2010

开发技术:

 ASP.net+Xml

课程总策划:

yuanbo

成员:

null

个人主页:

http://www.cnblogs.com/ylbtech/

科研团队:

ylbtech

教研团队:

ylbtech

 

3,ylb_menu:1, DemoReadXml ylb_tip:读Xml文档
 
3_1,/Vote.xml
<?xml version="1.0" encoding="utf-8"?>
<vote>
  <item belong="三国演义">
    <id>1</id>
    <name>赵云</name>
    <number>100</number>
  </item>
  <item belong="水浒">
    <id>2</id>
    <name>李逵</name>
    <number>4</number>
  </item>
  <item belong="三国演义">
    <id>3</id>
    <name>诸葛亮</name>
    <number>100</number>
  </item>
</vote>
 
3_2,/DemoReadXml.aspx.cs  
using System;

using System.Data;
public partial class DemoReadXml : System.Web.UI.Page
{
    /// <summary>
    /// ylb;1, 读取XML文档
    /// </summary>
    private void ReadXml()
    { 
        //创建一个虚拟库
        DataSet ds = new DataSet();
        //加载XML
        ds.ReadXml(Server.MapPath("Vote.xml"));

        //取出表
        DataTable dt = ds.Tables[0];

        //遍历表
        Response.Write("<pre>");
        //循环行
        for (int i = 0; i < dt.Rows.Count; i++)
        { 
            //循环列
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                //输出一列的值
                Response.Write(dt.Rows[i][j]+"\t");
            }
            Response.Write("\n");
        }
        Response.Write("</pre>");


        //把虚拟表给GridView
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //调用
        ReadXml();
    }
}

 

4,ylb_menu:2, DemoWriteXml
 
4_1,DemoWriteXml.aspx.cs ylb_tip:写Xml文档,Response写Xml文档
using System;

using System.Data;
using System.Data.SqlClient;
public partial class DemoWriteXml : System.Web.UI.Page
{

    /// <summary>
    /// ylb:1,把数据库中的表写成XML文档
    /// 数据读取器(DataReader)
    /// </summary>
    private void WriteXml()
    {
        string sql = "select top 10 productID,productName,unitPrice from Products";
        //string connStr = "Server=.;Database=Northwind;uid=sa;pwd=m123";
        string connStr = "Server=.;Database=Northwind;Integrated Security=SSPI";

        //创建一个虚拟库
        DataSet ds = new DataSet("Northwind");
        //创建一个虚拟表
        DataTable dt = new DataTable("Products");

        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand com = conn.CreateCommand();

        //把sql赋值给com.CommandText
        com.CommandText = sql;
        //peican

        conn.Open();

        //execute
        try
        {
            SqlDataReader sdr = com.ExecuteReader();

            //向虚拟表中加载数据
            dt.Load(sdr);
        }
        finally
        {
            conn.Close();
        }

        //把表添加到库中 
        ds.Tables.Add(dt);

        //写XML文档
        ds.WriteXml(Server.MapPath("Products.xml"));

        Response.Write("写Xml文档成功!");

    }

    
    /// <summary>
    /// ylb:2,把数据库中的表写成XML文档
    /// 用适配器(DataAdapter)
    /// </summary>
    private void WriteXml2()
    {
        string sql = "select top 10 productID,productName,unitPrice from Products";
        string connStr = "Server=.;Database=Northwind;Integrated Security=SSPI";

        //创建一个虚拟表
        DataSet ds = new DataSet("Northwind");

        SqlDataAdapter sdr = new SqlDataAdapter(sql, connStr);

        //用适配器去填充虚拟库
        sdr.Fill(ds);

        ds.Tables[0].TableName = "Products";    //给表格命名        

        //写XML文档
        ds.WriteXml(Server.MapPath("Products2.xml"));

        Response.Write("写Xml文档成功!");
    }

    /// <summary>
    /// ylb:2,把数据库中的表写成XML文档
    /// 用适配器(DataReader)
    /// </summary>
    private void WriteXml3()
    {
        string connStr = "Server=.;Database=Northwind;Integrated Security=SSPI";
        string sql = "select top 10 productId,productName,unitprice from products";


        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = connStr;

        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = sql;

        conn.Open();
        try
        {
            //直接输出Xml文档
            Response.ContentType = "text/xml";  //输出文档类型为xml
            Response.Write("<?xml version='1.0' encoding='UTF-8'?>");   //声明xml
            Response.Write("<Northwind>");   //起始根元素
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                //子元素
                Response.Write("<Products>");
                Response.Write(string.Format("<productId>{0}</productId>", sdr.GetInt32(0)));
                Response.Write(string.Format("<productName>{0}</productName>", sdr.GetString(1)));
                Response.Write(string.Format("<unitprice>{0}</unitprice>", sdr.GetDecimal(2)));
                Response.Write("</Products>");
            }
            Response.Write("</Northwind>"); //终止根元素

        }
        finally
        {
            conn.Close();
        }

        Response.End(); //停止前台页面输出
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //调用
        //ylb:1,
        //WriteXml();

        //ylb:2,
        //WriteXml2();

        //ylb:3,
        WriteXml3();
    }
}

 

5,示例|讲解案例下载

博客园讲解:

       http://ylbtech.cnblogs.com/

百度文库开发文档:

       http://passport.baidu.com/?business&aid=6&un=ylbtech#7

谷歌开源代码下载:

       http://code.google.com/p/ylbtechaspnet/downloads/list

请单击“ylbtechASPnetXml100010011”

 

warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

posted on 2012-08-16 16:04  ylbtech  阅读(1227)  评论(0编辑  收藏  举报