从数据库中读取XML数据

<script type="text/javascript"> </script> <script class="blogstory"> </script>

创建过程:创建SQL语句打开数据库连接,并调用ExecuteXmlReader() 方 法从数据库中读取数据,并返回一个XmlReader对象myxmlReader,然后用While语句循环读取XmlReader对象 myxmlReader中的XML片段,并添加XML文件的起始、结尾标志,构建一个XML文件,最后把该文件输出到页面ReaderXml.aspx上 面。

 

ReaderXml.aspx代码如下:


  1. <%@ Page Language= "C#"  AutoEventWireup= "true"   CodeFile= "ReaderXml.aspx.cs"  Inherits= "_Default"  %>  
  2.   
  3.   
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >  
  6.   
  7.   
  8.   
  9. <html xmlns="http://www.w3.org/1999/xhtml"  >  
  10.   
  11. <head runat="server" >  
  12.   
  13.     <title>从数据库中读取XML数据</title>  
  14.   
  15. </head>  
  16.   
  17. <body>  
  18.   
  19.     <form id="form1"  runat= "server" >  
  20.   
  21.     <div>  
  22.   
  23.       
  24.   
  25.     </div>  
  26.   
  27.     </form>  
  28.   
  29. </body>  
  30.   
  31. </html>  
  32. <strong><font color="#ff0000" >ReaderXml.aspx.cs代码如下:</font></strong><pre  class = "csharp"  name= "code" > using  System;  
  33.   
  34. using  System.Data;  
  35.   
  36. using  System.Configuration;  
  37.   
  38. using  System.Web;  
  39.   
  40. using  System.Web.Security;  
  41.   
  42. using  System.Web.UI;  
  43.   
  44. using  System.Web.UI.WebControls;  
  45.   
  46. using  System.Web.UI.WebControls.WebParts;  
  47.   
  48. using  System.Web.UI.HtmlControls;  
  49.   
  50. using  System.Data.SqlClient;  
  51.   
  52. using  System.Xml;  
  53.   
  54.   
  55.   
  56. public  partial  class  _Default : System.Web.UI.Page   
  57.   
  58. {  
  59.   
  60.     protected   void  Page_Load( object  sender, EventArgs e)  
  61.   
  62.     {  
  63.   
  64.         if (!Page.IsPostBack)  
  65.   
  66.         {   ///从数据库读取XML数据   
  67.   
  68.             ReaderXmlData();  
  69.   
  70.         }  
  71.   
  72.     }  
  73.   
  74.   
  75.   
  76.     private   void  ReaderXmlData()  
  77.   
  78.     {  
  79.   
  80.         ///创建链接   
  81.   
  82.         SqlConnection myConnection = new  SqlConnection(  
  83.   
  84.             ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING" ].ConnectionString);  
  85.   
  86.         ///定义SQL语句   
  87.   
  88.         string  cmdText =  "SELECT * FROM Users FOR XML AUTO" ;  
  89.   
  90.         ///创建Command   
  91.   
  92.         SqlCommand myCommand = new  SqlCommand(cmdText,myConnection);  
  93.   
  94.         try   
  95.   
  96.         {  
  97.   
  98.             ///打开连接   
  99.   
  100.             myConnection.Open();  
  101.   
  102.   
  103.   
  104.             XmlReader myxmlReader = myCommand.ExecuteXmlReader();  
  105.   
  106.   
  107.   
  108.             ///移动到XML元素处   
  109.   
  110.             myxmlReader.MoveToElement();  
  111.   
  112.             ///输出XML文件的标志   
  113.   
  114.             Response.Write("<?xml version='1.0'?>" );  
  115.   
  116.             ///输出父节点   
  117.   
  118.             Response.Write("<Users>" );  
  119.   
  120.             ///读取从数据库中获取的数据   
  121.   
  122.             while (myxmlReader.IsStartElement())  
  123.   
  124.             {  
  125.   
  126.                 ///显示从数据库中获取的数据   
  127.   
  128.                 Response.Write(myxmlReader.ReadOuterXml());  
  129.   
  130.             }  
  131.   
  132.             Response.Write("</Users>" );  
  133.   
  134.             ///关闭XMLReader   
  135.   
  136.             myxmlReader.Close();  
  137.   
  138.         }  
  139.   
  140.         catch (SqlException sqlex)  
  141.   
  142.         {  
  143.   
  144.             ///显示链接错误的消息   
  145.   
  146.             Response.Write(sqlex.Message + "<br>" );  
  147.   
  148.         }  
  149.   
  150.         finally   
  151.   
  152.         {  
  153.   
  154.             ///关闭数据库的链接   
  155.   
  156.             myConnection.Close();  
  157.   
  158.         }  
  159.   
  160.         Response.End();  
  161.   
  162.     }  
  163.   
  164. }  
  165.   
  166. </pre> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值