How Can write a DataSet as XML through VB.Net & C#?

XMLToday I am going to talk about XML again, today we learn how to make XML from a Dataset.

There are 3 different methods that you can chose, but before producing a XML file or XML Content we need to produce a Dataset.


  Private Sub MakeDataSet()
        Dim ds As New DataSet()
        Dim OConnString As String = "Server=CodingTips;Database=_
                                   Library;" + _
                                   "Integrated Security=true;"

        Using conn As New SqlConnection(OConnString)
            Using cmd As SqlCommand = conn.CreateCommand()

                cmd.CommandText = "SELECT * FROM Books;"
                Dim Oda As New SqlDataAdapter(cmd)
            End Using
        End Using
    End Sub


private void MakeDataSet()
    DataSet ds = new DataSet();
    string OConnString = "Server=CodingTips;Database=Library;" + _
"Integrated Security=true;";

    using (SqlConnection conn = new SqlConnection(OConnString)) {
        using (SqlCommand cmd = conn.CreateCommand()) {

            cmd.CommandText = "SELECT * FROM Books;";
            SqlDataAdapter Oda = new SqlDataAdapter(cmd);

Now you are ready to produce XML File,follow these three different methods.
1. Write XML directly in the film





2.The second method is GetXml , the result of this method is String but it does not support any of the advanced features of the XML serialization


Dim strxml as string = ds.GetXml()


string strxml = ds.GetXml();

3.Sometimes you want to use all advanced features of the XML serialization but you don’t want to save the file, in this case you can use this solution.


Dim strm As New MemoryStream()


MemoryStream strm = new MemoryStream();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s