转换DataSet到普通xml的新法

来源:互联网 发布:无权限访问网络打印机 编辑:程序博客网 时间:2024/04/29 19:45
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
大家知道,用DataSet传递的WebService,微软会在各个节点加上schema,所以无法与j2ee,flash兼容,所以我找到了一种转换他们变成普通xml的方法。代码如下:

方法一:
Public Class DataSetToxml : Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objConn As SqlConnection
Dim strSql As String

strSql = "SELECT TOP 10 * FROM Customers"
objConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))

Dim sdaCust As New SqlDataAdapter(strSql, objConn)
Dim dstCust As New DataSet()

sdaCust.Fill(dstCust, "Customers")
'Save data to xml file and schema file
dstCust.Writexml(Server.MapPath("Customers.xml"),xmlWriteMode.IgnoreSchema)
dstCust.WritexmlSchema(Server.MapPath("Customers.xsd"))
End Sub

这种方法是写入一个xml文件


方法二:
<WebMethod(Description:="所有教室列表")> _
Public Function ListAllRooms() As xmlDocument

Try
m_CpCourseArrange.FillRoomId(m_DsCourseArrange)
'Dim reader As New MemoryStream


Dim doc As New xmlDocument
doc.Loadxml(m_DsCourseArrange.Getxml.ToString)
Return doc

Catch ex As Protocols.SoapException
Throw SoapExceptionE.RaiseException("ListAllRooms", "http://tempuri.org/CourseArrange", ex.Message, "4000", ex.Source, SoapExceptionE.FaultCode.Server)
End Try
End Function


Getxml--Returns the xml representation of the data stored in the DataSet. (MSDN)


Private Shared Sub DemonstrateGetxml()
' Create a DataSet with one table containing two columns and 10 rows.
Dim ds As DataSet = New DataSet("myDataSet")
Dim t As DataTable = ds.Tables.Add("Items")
t.Columns.Add("id", Type.GetType("System.Int32"))
t.Columns.Add("Item", Type.GetType("System.String"))

' Add ten rows.
Dim r As DataRow
Dim i As Integer
For i = 0 To 9
r = t.NewRow()
r("id") = i
r("Item")= "Item" & i
t.Rows.Add(r)
Next

' Display the DataSet contents as xml.
Console.WriteLine( ds.Getxml() )
End Sub


看来以后用DataSet传递的时候也不用为它的转换发愁了。

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击