打印

[asp.net教程] 转换datase t到普通xml的新法

转换datase t到普通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文件
   
   
  方法二:
   <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传递的时候也不用为它的转换发愁了。

TOP

返回顶部
AYBlue

Processed in 0.055104 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 10:55 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP