当前位置:首页 > 网络编程 > WEB编程 > ASP.net > 转换DataSet到普通xml的新法

转换DataSet到普通xml的新法

点击次数:32 次 发布日期:2008-11-21 22:20:55 作者:源代码网
源代码网推荐

源代码网整理以下大家知道,用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传递的时候也不用为它的转换发愁了。

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华