转换DataSet到普通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文件

方法二:

 1<webmethod(description:="所有教室列表")> _   
 2Public Function ListAllRooms() As XmlDocument 
 3
 4Try   
 5m_CpCourseArrange.FillRoomId(m_DsCourseArrange)   
 6'Dim reader As New MemoryStream 
 7
 8  
 9Dim doc As New XmlDocument   
10doc.LoadXml(m_DsCourseArrange. **GetXml.ToString** )   
11Return doc 
12
13Catch ex As Protocols.SoapException   
14Throw SoapExceptionE.RaiseException("ListAllRooms", " http://tempuri.org/CourseArrange ", ex.Message, "4000", ex.Source, SoapExceptionE.FaultCode.Server)   
15End Try   
16End Function   
17  
18  
19GetXML--Returns the XML representation of the data stored in the DataSet . (MSDN)   
20
21
22Private Shared Sub DemonstrateGetXml()   
23' Create a DataSet with one table containing two columns and 10 rows.   
24Dim ds As DataSet = New DataSet("myDataSet")   
25Dim t As DataTable = ds.Tables.Add("Items")   
26t.Columns.Add("id", Type.GetType("System.Int32"))   
27t.Columns.Add("Item", Type.GetType("System.String")) 
28
29' Add ten rows.   
30Dim r As DataRow   
31Dim i As Integer   
32For i = 0 To 9   
33r = t.NewRow()   
34r("id") = i   
35r("Item")= "Item" &amp; i   
36t.Rows.Add(r)   
37Next 
38
39' Display the DataSet contents as XML.   
40Console.WriteLine( ds.GetXml() )    
41End Sub   
42  
43  
44看来以后用dataset传递的时候也不用为它的转换发愁了。 ![](http://blog.csdn.net/Emoticons/teeth_smile.gif)</webmethod(description:="所有教室列表")>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus