转换DataSet到普通xml的新法_.Net教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:在ASP.NET中跨页面实现多选
本文介绍如何在ASP.NET中实现多页面选择的问题。其具体思路很简单:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时,检查保存的值,再在DataGrid中进行选中显示。下面时完整的代码和例子:

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

分享:ASP.Net中保护自定义的服务器控件
自定义服务器控件是扩展 ASP.NET Web 服务器控件的功能的一种方式。下文提供了针对自定义服务器控件的用户和开发人员的基本安全准则。有关创建自定义服务器控件的更多信息,请参见开发自定义 AS

来源:模板无忧//所属分类:.Net教程/更新时间:2008-08-22
相关.Net教程