Working in XML file in ASP.NET C#

XML is a general purpose tag based language and very easy to transfer and store data across applications. Like HTML , XML is a subset of SGML (Standard Generalized Markup Language). XML is a platform independent language, so the information formatted in XML can be used in any other platforms (Operating Systems). XML is a self describing language and it gives the data as well as the rules to identify what information it contains.

XML files are made up of tags that contains data. Generally a start tag and end tag to hold the data. For example, if you want to create an XML tag name "Header" , the start tag is like < Header > and the end tag is like < /Header > . We can fill our information between these tags.
< Header > Header Content Here < /Header >
While creating an XML file , some important points have to remember :
* XML is case sensitive
ex: < Header > is not same as < HeadeR > .

Now i will tell you how to pass the database from sql to xml
after just create XMLfile use this steps

1. Take one button,label  in your web form and give that label name to showmessage and button to save.

2. And now double click on  save button and write this code and before this code don't forget to use your own connection string


 protected void  Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=VAIO\SQLEXPRESS;Initial Catalog=Studentreg;Integrated Security=True;");

            string strSQL = "Select *  from course";

            SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);

            DataSet ds = new DataSet();

            dt.Fill (ds,"course");

            ds.WriteXml(Server.MapPath("XMLFile.xml"));

           LabelMessage.Text = "<a href=XMLFile.xml> XML file</a>";
           con.Close();

}

the area where i highlight the code in bold that means it will provide link to open the xmlfile in runtime and output will like this:

<NewDataSet><course><course_id>2</course_id><c_name>ASP.NET</c_name><duration>3</duration><c_fee>8000</c_fee></course><course><course_id>3</course_id><c_name>Java</c_name><duration>3</duration><c_fee>10000</c_fee></course><course><course_id>4</course_id><c_name>ASP</c_name><duration>3</duration><c_fee>8000</c_fee></course><course><course_id>5</course_id><c_name>J2EE</c_name><duration>3</duration><c_fee>10000</c_fee></course></NewDataSet>

that is in xmlfile to get this database in xml from sql you have to click on save button and then it will provide you link via label as "XML file" and click on that and you will got this It is very important concept try this.....bye

No comments:

Post a Comment