DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 轉換DataSet到普通xml的新法 - asp.net
轉換DataSet到普通xml的新法 - asp.net
編輯:XML詳解     
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傳遞的時候也不用為它的轉換發愁了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved