ARTICLE

Fetching Data from a Database in XML Format

Posted by Ashish Singhal Articles | Visual C# January 28, 2006
In this article we will see how to fetch data from database in XML format.
Reader Level:

Here we will see that how can we fetch data from database in XML format.

using System;

using System.Data.SqlClient;

using System.Xml;

 

namespace test

{

          /// <summary>

          /// Summary description for Class1.

          /// </summary>

          class ReadXML

          {

                    /// <summary>

                    /// The main entry point for the application.

                    /// </summary>

                    [STAThread]

                    static void Main(string[] args)

                    {

                             //

                             string connectionString = "server=.;uid=test;pwd=test;database=test";

                             SqlConnection con = new SqlConnection(connectionString);

                             SqlCommand cmd = new SqlCommand("select * from test for xml auto, elements", con);

                             con.Open();

                             XmlReader reader = cmd.ExecuteXmlReader();

                             con.Close();

                             XmlDocument doc = new XmlDocument();

                             doc.LoadXml("<root></root>");

                             XmlNode node = doc.ReadNode(reader);

                             while(node != null)

                             {

                                       Console.WriteLine(node.InnerXml);

                                       node = doc.ReadNode(reader);

                             }

 

                             reader.Close();

                             reader = null;

                             doc = null;

                             //

                    }

          }

}

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter