转换DataSet到普通xml的新法
2024-09-05 20:55:52
供稿:网友
大家知道,用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传递的时候也不用为它的转换发愁了。