Publish date: 2003-04-23

ADO.Net Objects. Read and Write XML (C#, VB.Net)

Download the example archive
 using System;
using System.Data;
using System.Data.OleDb;   

namespace CSharp
{
    /// <summary>
    /// This sample demonstrate how to work with XML from ADO.Net using OLE DB IBProvider
    /// </summary>
    class ADONetSample_ReadWriteXMLData
    {
        [STAThread]
        static void Main(string[] args)        
        {
            try
            {
            //open connection from udl file
            OleDbConnection myConnection = new OleDbConnection("file name=employee.udl");

            //two OleDBCommand for DataAdapters
            OleDbCommand mySelectCmd1 = new OleDbCommand("select * from customer", myConnection);
            OleDbCommand mySelectCmd2 = new OleDbCommand("select * from country", myConnection);

            OleDbDataAdapter myOleDBDataAdapter1 = new OleDbDataAdapter(mySelectCmd1);
            OleDbDataAdapter myOleDBDataAdapter2 = new OleDbDataAdapter(mySelectCmd2);

            OleDbTransaction myTrans;

            //new DataSet
            DataSet myDataSet= new DataSet("Sample DataSet");

            //open conneciton
            myConnection.Open();
            myTrans = myConnection.BeginTransaction(); //start transaction

            //set Command Transaction
            mySelectCmd1.Transaction = myTrans;
            mySelectCmd2.Transaction = myTrans;

            //fill myDataSet     
            myOleDBDataAdapter1.Fill(myDataSet, "Customers");
            myOleDBDataAdapter2.Fill(myDataSet, "Country");

            string strXMLData;
            string strXMLSchema;

            //get XML Data and Schema
            strXMLData = myDataSet.GetXml();
            strXMLSchema = myDataSet.GetXmlSchema();

            Console.WriteLine("XML Schema");
            Console.WriteLine("-----------------------------");
            Console.WriteLine(strXMLSchema);

            Console.WriteLine("-----------------------------");
            Console.WriteLine("XML Data");
            Console.WriteLine("-----------------------------");
            Console.WriteLine(strXMLData);
        
            }
            catch(OleDbException myOLEDBException)
            { 
                //ole db exception
                //error collection
                Console.WriteLine("----------------------------------------");
                for (int i = 0; i<=myOLEDBException.Errors.Count-1;i++)
                {
                    Console.WriteLine("Message " + (i + 1) + ": " + myOLEDBException.Errors[i].Message);
                    Console.WriteLine("Native: " + myOLEDBException.Errors[i].NativeError.ToString());
                    Console.WriteLine("Source: " + myOLEDBException.Errors[i].Source);
                    Console.WriteLine("SQL: " + myOLEDBException.Errors[i].SQLState);
                    Console.WriteLine("----------------------------------------");
                }
            }
            catch (Exception myException)
            {
                //other exception                
                Console.WriteLine("----------------------------------------");
                Console.WriteLine(myException.Message);
            }
            
            Console.WriteLine("All Done. Press Enter to close");
            Console.ReadLine();
        }
    }
}
Download the example archive



Download the example archive
 Imports System.Data.OleDb

'This sample demonstrate how to work with XML from ADO.Net using OLE DB IBProvider
Module ADONetSample_ReadWriteXML

    Sub Main()
        Try
            'open connection from udl file
            Dim myConnection As OleDbConnection = New OleDbConnection("file name=employee.udl")

            'two OleDBCommand for DataAdapters
            Dim mySelectCmd1 As OleDbCommand = New OleDbCommand("select * from customer", myConnection)
            Dim mySelectCmd2 As OleDbCommand = New OleDbCommand("select * from country", myConnection)

            Dim myOleDBDataAdapter1 As New OleDbDataAdapter(mySelectCmd1)
            Dim myOleDBDataAdapter2 As New OleDbDataAdapter(mySelectCmd2)

            Dim myTrans As OleDbTransaction

            'new DataSet
            Dim myDataSet As New DataSet("Sample DataSet")

            'open conneciton
            myConnection.Open()
            myTrans = myConnection.BeginTransaction() 'start transaction

            'set Command Transaction
            mySelectCmd1.Transaction = myTrans
            mySelectCmd2.Transaction = myTrans

            'fill myDataSet     
            myOleDBDataAdapter1.Fill(myDataSet, "Customers")
            myOleDBDataAdapter2.Fill(myDataSet, "Country")

            Dim strXMLData As String
            Dim strXMLSchema As String

            'get XML Data and Schema
            strXMLData = myDataSet.GetXml()
            strXMLSchema = myDataSet.GetXmlSchema()

            Console.WriteLine("XML Schema")
            Console.WriteLine("-----------------------------")
            Console.WriteLine(strXMLSchema)

            Console.WriteLine("-----------------------------")
            Console.WriteLine("XML Data")
            Console.WriteLine("-----------------------------")
            Console.WriteLine(strXMLData)

            'Ole Db Error Collection                
        Catch myOLEDBException As OleDbException
            Dim str As String, i As Integer

            For i = 0 To myOLEDBException.Errors.Count - 1
                Console.WriteLine("----------------------------------------")
                Console.WriteLine("Message " & i.ToString + 1 & ": " + myOLEDBException.Errors(i).Message, True)
                Console.WriteLine("Native: " + myOLEDBException.Errors(i).NativeError.ToString())
                Console.WriteLine("Source: " + myOLEDBException.Errors(i).Source)
                Console.WriteLine("SQL: " + myOLEDBException.Errors(i).SQLState)
                Console.WriteLine("----------------------------------------")
            Next i

            'Other Error
        Catch e As Exception
            Console.WriteLine("----------------------------------------")
            Console.WriteLine(e.Message)

        End Try

        Console.WriteLine("All Done. Press Enter to close")
        Console.ReadLine()

    End Sub

End Module
Download the example archive