Reader Level:
Articles

Fetching Data from a Database in XML Format

By Ashish Singhal on January 28, 2006
In this article we will see how to fetch data from database in XML format.
  • 0
  • 0
  • 25776

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;

                             //

                    }

          }

}

Ashish Singhal

Ashish has been working as a software engineer since 2002. He has written articles for C# Corner . He specializes in the implementation of client/server, database, graphics and/or Internet-based systems using Visual Stud... Read more

COMMENT USING

Trending up