Data Export XML in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source

This sample demonstrates how to export data table into xml file.

private void btnXML_Click(object sender, EventArgs e)
{
    System.Data.OleDb.OleDbConnection oleDbConnection1
         = new System.Data.OleDb.OleDbConnection();
    oleDbConnection1.ConnectionString
        = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb";

    System.Data.OleDb.OleDbCommand oleDbCommand1
        = new System.Data.OleDb.OleDbCommand();
    oleDbCommand1.CommandText = "select * from parts";
    oleDbCommand1.Connection = oleDbConnection1;

    Spire.DataExport.XML.XMLExport xmlExport1
        = new Spire.DataExport.XML.XMLExport();

    xmlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
    xmlExport1.DataFormats.CultureName = "zh-CN";
    xmlExport1.DataFormats.Currency = "c";
    xmlExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
    xmlExport1.DataFormats.Float = "g";
    xmlExport1.DataFormats.Integer = "g";
    xmlExport1.DataFormats.Time = "H:mm";
    xmlExport1.FileName = "sample.xml";
    xmlExport1.SQLCommand = oleDbCommand1;

    oleDbConnection1.Open();
    xmlExport1.SaveToFile();
}

Private Sub btnXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXML.Click
    Dim oleDbConnection1 As New System.Data.OleDb.OleDbConnection()
    oleDbConnection1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\..\Database\demo.mdb"

    Dim oleDbCommand1 As New System.Data.OleDb.OleDbCommand()
    oleDbCommand1.CommandText = "select * from parts"
    oleDbCommand1.Connection = oleDbConnection1

    Dim xmlExport1 As New Spire.DataExport.XML.XMLExport()

    xmlExport1.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView
    xmlExport1.DataFormats.CultureName = "zh-CN"
    xmlExport1.DataFormats.Currency = "c"
    xmlExport1.DataFormats.DateTime = "yyyy-M-d H:mm"
    xmlExport1.DataFormats.Float = "g"
    xmlExport1.DataFormats.[Integer] = "g"
    xmlExport1.DataFormats.Time = "H:mm"
    xmlExport1.FileName = "sample.xml"
    xmlExport1.SQLCommand = oleDbCommand1

    oleDbConnection1.Open()
    xmlExport1.SaveToFile()
End Sub