Generating the XML file

There are many ways you could dynamically generate an XML file from a database. The following example was created using C# code running in IIS, connecting to a local SQL Server 2000 database called agdb, using a user called web, and retrieving data from a table called Employees.

C# sample code to generate XML file from back-end data
SqlConnection myConnection = 
    new SqlConnection("Initial Catalog=agdb;" + 
    "Data Source=localhost;" 
    + "user id=web; password=web" );
         SqlCommand myCommand = 
            new SqlCommand(
                "SELECT id, lastname, firstname, 
                phonenumber " + "FROM Employees", 
                myConnection); myConnection.Open(); 

                // read the data 
                SqlDataReader dr = myCommand.ExecuteReader();

                // write out the header for the xml response 
                Response.Output.WriteLine("<?xml version=\"1.0\" 
                    " + "encoding=\"utf-8\" ?>"); 
                Response.Output.WriteLine("<root>"); 
                // write out xml for data from database 
                while (dr.Read()) 
                { 
                    Response.Output.WriteLine( 
                        "<Employees id=\"" + 
                            dr.GetInt32(0).ToString() + "\" 
                            lastname=\"" + dr.GetString(1) + "\" 
                            firstname=\"" + dr.GetString(2) + "\" 
                            phonenumber=\"" + dr.GetString(3) + "\" />"); 
                } 
                Response.Output.WriteLine("</root>"); 

                // close the connection to the database 
                myConnection.Close();
Note

You can find the sample code shown in the figure above in the list.aspx.cs file, which is available in DB_sample.zip file on the M-Business Anywhere Product Manuals page, referenced in Related publications.