将数据表导出到 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:04:18  来源:igfitidea点击:

Export Datatable to xml

xmlvb.netado.net

提问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()输出的内容DataTablesXML

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:

你需要得到一个stringXML为了使用它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 the DataSet.

    Dim simpleresult As String = ds.GetXml
    

    Output:

    <NewDataSet>
      <Table1>
        <Column1>Value1</Column1>
        <Column2>Value2</Column2>
      </Table1>
      <Table1>
        <Column1>Value3</Column1>
        <Column2>Value4</Column2>
      </Table1>
    </NewDataSet>
    
  • You can use WriteXmlmethods from the DataSetor a DataTableif you want to control XmlWriteMode. You get the Stringusing a MemoryStream.

    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 Using
    

    Output:

    <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方法。您可以使用.DataSetDataTableXmlWriteModeStringMemoryStream

    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 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>