将数据表导出到 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17155071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export Datatable to xml
提问by user1707572
I have some DataTablesthat I want to export to a xmlfile. I can use DataTable.WriteXml()to output the contents of the DataTablesto XML.
我有一些DataTables我想导出到xml文件中。我可以使用DataTable.WriteXml()输出的内容DataTables来XML。
I need to use a Response objectas shown. I need to add attributes to the root of the output xml. Please help me with that. here is the code which I'm working on.
我需要使用Response object如图所示的。我需要向输出 xml 的根添加属性。请帮我解决这个问题。这是我正在处理的代码。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim FileName1 As String = "Sheet1.xml"
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName1)
Response.ContentType = "text/xml"
Dim ds As New DataSet("BuildingGroups")
ds.EnforceConstraints = False
Dim dtBuildingGroup As DataTable = Pipeline.Products.Components.BuildingGroupManager.GetBuildingGroupsToXML
Dim result As String
Dim sw As New StringWriter()
dtBuildingGroup.TableName = "BuildingGroup"
ds.Tables.Add(dtBuildingGroup)
'Dim doc As New XmlDataDocument(ds)
ds.WriteXml(sw)
result = sw.ToString()
Response.Write(result)
Response.End()
End Sub
回答by Chris
You need to get a stringfrom the XMLin order to use it in Response.Write(result), here are some ways:
你需要得到一个string从XML为了使用它Response.Write(result),这里有一些方法:
Suppose a small DataSetcreated like this:
假设一个DataSet像这样创建的小:
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Column1"))
dt.Columns.Add(New DataColumn("Column2"))
Dim r = dt.NewRow
r.Item(0) = "Value1"
r.Item(1) = "Value2"
dt.Rows.Add(r)
r = dt.NewRow
r.Item(0) = "Value3"
r.Item(1) = "Value4"
dt.Rows.Add(r)
Dim ds As New DataSet
ds.Tables.Add(dt)
You can use
GetXmlmethods from theDataSet.Dim simpleresult As String = ds.GetXmlOutput:
<NewDataSet> <Table1> <Column1>Value1</Column1> <Column2>Value2</Column2> </Table1> <Table1> <Column1>Value3</Column1> <Column2>Value4</Column2> </Table1> </NewDataSet>You can use
WriteXmlmethods from theDataSetor aDataTableif you want to controlXmlWriteMode. You get theStringusing aMemoryStream.Dim result As String Using ms As New IO.MemoryStream() ds.WriteXml(ms, System.Data.XmlWriteMode.WriteSchema) result = System.Text.Encoding.UTF8.GetString(ms.ToArray) End UsingOutput:
<NewDataSet> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Table1"> <xs:complexType> <xs:sequence> <xs:element name="Column1" type="xs:string" minOccurs="0" /> <xs:element name="Column2" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <Table1> <Column1>Value1</Column1> <Column2>Value2</Column2> </Table1> <Table1> <Column1>Value3</Column1> <Column2>Value4</Column2> </Table1> </NewDataSet>
您可以使用
GetXml从方法DataSet。Dim simpleresult As String = ds.GetXml输出:
<NewDataSet> <Table1> <Column1>Value1</Column1> <Column2>Value2</Column2> </Table1> <Table1> <Column1>Value3</Column1> <Column2>Value4</Column2> </Table1> </NewDataSet>如果要控制 ,可以使用或 a 中的
WriteXml方法。您可以使用.DataSetDataTableXmlWriteModeStringMemoryStreamDim result As String Using ms As New IO.MemoryStream() ds.WriteXml(ms, System.Data.XmlWriteMode.WriteSchema) result = System.Text.Encoding.UTF8.GetString(ms.ToArray) End Using输出:
<NewDataSet> <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:complexType> <xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:element name="Table1"> <xs:complexType> <xs:sequence> <xs:element name="Column1" type="xs:string" minOccurs="0" /> <xs:element name="Column2" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <Table1> <Column1>Value1</Column1> <Column2>Value2</Column2> </Table1> <Table1> <Column1>Value3</Column1> <Column2>Value4</Column2> </Table1> </NewDataSet>

