Creating XML file using StreamWriter

We can easily create XML file using StreamWriter

Find Sample code below::



string xmlString = string.Empty;


SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(stringName, cn);
SqlDataReader dr;
cmd.ExecuteNonQuery();
xmlString += " " ;
xmlString += "";
while (dr.Read())
{
xmlString += "";
xmlString += dr[0].ToString();
xmlString += "
";
xmlString += dr[1].ToString();
xmlString += "
";

}
xmlString += "
";
StreamWriter streamWriter = File.CreateText("D:\ampl.xml");
streamWriter.WriteLine(xmlString);
streamWriter.Close();




cheers


Comments

Author: Lion17 Mar 2010 Member Level: Gold   Points : 1

please right code....

remove this line...
// cmd.ExecuteNonQuery();


and add this line...

cn.Open();
dr = cmd.ExecuteReader();

it's will be correct.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: