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

ASP.NET
.Net业务平台的数值精度陷阱与解决方法
投票结果的图片进度条显示
七招制胜ASP.NET应用程序开发
ASP.NET 2.0的导航控件treeview和menu的实例
ASP.NET实现页面间值传递的几种方法
项目调试时出现用到的一个组件“访问被拒绝”的解决方法
ASP.NET中“找不到指定模块”的解决办法
C#中的委托和事件
在指定应用程序域中执行代码
Ajax简单客户登陆验证
AJAX将成为移动Web2.0时代首选开发平台
关闭按钮点击事件的捕捉
远程抓取GOOGLE的自动翻译结果
保护你的DLL和Code不被别人使用
ASP.NET的底层的工作机制介绍
ASP.NET2.0服务器控件之捕获回传事件
使用Data Access Application Block 得到存储过程的返回值
面向服务及其在互联系统策略中的角
ASP.NET2.0服务器控件之创建复杂属性
ASP.NET:设置页面buffer引出来的问题

ASP.NET 中的 转换DataSet到普通xml的新法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 49 ::
收藏到网摘: n/a

大家知道,用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传递的时候也不用为它的转换发愁了。