服务器端生成xml的方法

1)      方法一:利用XmlReader,以流的方式生成

ExpandedBlockStart.gif ContractedBlock.gif Public   Function GetArchives() Function GetArchives() As String
InBlock.gif
InBlock.gif        
Dim cSql As String
InBlock.gif
InBlock.gif        
Dim Constr_ZT As String = Application("ConStr"& "zt" & Session("LOGZTID"& "sysinfo"
InBlock.gif
InBlock.gif        
Dim Cycn As New SqlClient.SqlConnection(Constr_ZT)
InBlock.gif
InBlock.gif        
Dim Cycmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
InBlock.gif
InBlock.gif        
Dim CydSet As DataSet = New DataSet
InBlock.gif
InBlock.gif        
Dim Cyadp As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cSql, Cycn)
InBlock.gif
InBlock.gif
Dim Cysb As System.Text.StringBuilder = New System.Text.StringBuilder
InBlock.gif
InBlock.gif        
Try
InBlock.gif
InBlock.gif            Cycn.Open()
InBlock.gif
InBlock.gif            Cycmd.Connection 
= Cycn
InBlock.gif
InBlock.gif            Cysb 
= New System.Text.StringBuilder
InBlock.gif
InBlock.gif            Cyadp.SelectCommand.Connection 
= Cycn
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
'客户档案 
InBlock.gif

InBlock.gif            cSql 
= "SELECT * FROM customer_list ORDER BY frem"
InBlock.gif
InBlock.gif            Cyadp.SelectCommand.CommandText 
= cSql
InBlock.gif
InBlock.gif            Cyadp.Fill(CydSet, 
"cus")
InBlock.gif
InBlock.gif            
'供应商
InBlock.gif

InBlock.gif            cSql 
= "SELECT * FROM vendor_list ORDER BY frem"
InBlock.gif
InBlock.gif            Cyadp.SelectCommand.CommandText 
= cSql
InBlock.gif
InBlock.gif            Cyadp.Fill(CydSet, 
"pro")
InBlock.gif
InBlock.gif           
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
'为各表字段设置属性类型,如不设置将以节点的形式返回
InBlock.gif

InBlock.gif            
Dim i, Ci As Integer
InBlock.gif
InBlock.gif            
For i = 0 To CydSet.Tables.Count - 1 Step 1
InBlock.gif
InBlock.gif                
For Ci = 0 To CydSet.Tables(i).Columns.Count - 1 Step 1
InBlock.gif
InBlock.gifCydSet.Tables(i).Columns(CydSet.Tables(i).Columns(Ci).ColumnName).ColumnMapping 
= MappingType.Attribute
InBlock.gif
InBlock.gif                
Next
InBlock.gif
InBlock.gif            
Next
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
'生成xml数据
InBlock.gif

InBlock.gif            Cysb.Append(
Replace(CydSet.GetXml, "NewDataSet""root"))
InBlock.gif
InBlock.gif            CydSet.Reset()
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
Return Cysb.ToString()  
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
Catch ex As Exception
InBlock.gif
InBlock.gif            
If Cycn.State = ConnectionState.Open Then Cycn.Close()
InBlock.gif
InBlock.gif            
Return ""
InBlock.gif
InBlock.gif        
End Try
InBlock.gif
ExpandedBlockEnd.gif    
End Function

None.gif
None.gif 
None.gif
None.gif

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

例:

 

 

For XML

 SQL 语句表达式:

SELECT

FROM

WHERE

ORDER BY

FOR XML (raw | auto [, ELEMENTS] |

                  explicit)

        [, XMLData]

        [, BINARY base64])

 

 

 

 

for xml raw”与“for xml auto”的区别:

n    for xml raw

¨           在结果集中每一行有一个元素

¨           没有嵌套的子元素

¨           在结果集中的列/值表示成元素的特性/

¨           和数据库的结果集很类似,但表示成XML 格式,很容易转换

n    for xml auto

¨           数据库中的表/视图名表示成元素名

¨           列名用来表示特性,如果加ELEMENTS 选项,列表示成子元素

¨           支持嵌套的 XML 输出结果,结果集中的层次结构(元素的嵌套)基于 SELECT 子句中指定的列所标识的表顺序

¨           可以用表和列别名来改变元素和特性名

 

 

2)      方法二:利用DataSet,以块的方式生成


ExpandedBlockStart.gif ContractedBlock.gif Public   Function A() Function A() As String
InBlock.gif
InBlock.gif        
Dim cSql As String
InBlock.gif
InBlock.gif        
Dim Constr_ZT As String = Application("CONSTR"& "zt" & Session("LOGZTID"& "sysinfo" 
InBlock.gif
InBlock.gif        
Dim Cycn As New SqlClient.SqlConnection(Constr_ZT)
InBlock.gif
InBlock.gif        
Dim Cycmd As New SqlClient.SqlCommand("", Cycn)
InBlock.gif
InBlock.gif        
Try
InBlock.gif
InBlock.gif            Cycn.Open()
InBlock.gif
InBlock.gif            Cycmd.Connection 
= Cycn
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
Dim Cyreader As System.Xml.XmlReader = Cycmd.ExecuteXmlReader()
InBlock.gif
InBlock.gif            
Dim Cysb As System.Text.StringBuilder = New System.Text.StringBuilder
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            Cysb.Append(
"")
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            cSql 
= "select * from customer_list order by frem for xml raw"
InBlock.gif
InBlock.gif            Cycmd.CommandText 
= cSql
InBlock.gif
InBlock.gif            Cyreader.Read()
InBlock.gif
InBlock.gif            
While Not Cyreader1.EOF
InBlock.gif
InBlock.gif                Cysb.Append(Cyreader.ReadOuterXml())
InBlock.gif
InBlock.gif            
End While
InBlock.gif
InBlock.gif            Cyreader.Close()
InBlock.gif
InBlock.gif            Cycn.Close()
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gifcSql 
= "select * from vendor_list order by frem for xml raw"
InBlock.gif
InBlock.gif            Cycmd.CommandText 
= cSql
InBlock.gif
InBlock.gif            Cyreader.Read()
InBlock.gif
InBlock.gif            
While Not Cyreader1.EOF
InBlock.gif
InBlock.gif                Cysb.Append(Cyreader.ReadOuterXml())
InBlock.gif
InBlock.gif            
End While
InBlock.gif
InBlock.gif            Cyreader.Close()
InBlock.gif
InBlock.gif            Cycn.Close()
InBlock.gif
InBlock.gif            Cysb.Append(
"")
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
Return Cysb.ToString()   ''形成XML数据岛
InBlock.gif

InBlock.gif        
Catch ex As Exception
InBlock.gif
InBlock.gif            
If Cycn.State = ConnectionState.Open Then Cycn.Close()
InBlock.gif
InBlock.gif            
Return ""
InBlock.gif
InBlock.gif        
End Try
InBlock.gif
ExpandedBlockEnd.gif
End Function

None.gif
None.gif


3)      方法一与方法二的比较

n    方法一适合小批量的数据查询,当数据量较大的时候,方法二的速度会快些。

n    web 服务器资源要求:方法一低,方法二高。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值