Read from SQL Table and Write as XML File:
string Coon = ConfigurationManager.ConnectionStrings["ConnectionSQL"].ConnectionString;
SqlDataAdapter DA = null;
DataSet ds = new DataSet();
DataTable dt = null;
SqlConnection Con = new SqlConnection(Coon);
string Query = "SELECT ID,NAME,SALARY,CITY FROM Customer FOR XML RAW('Customer'), ROOT('Customers'), ELEMENTS";
XmlReader reader;
XmlDocument xmlDoc;
SqlCommand cmd = null;
cmd = new SqlCommand(Query, Con);
Con.Open();
reader = cmd.ExecuteXmlReader();
xmlDoc = new XmlDocument();
while (reader.Read())
{
xmlDoc.Load(reader);
}
if (File.Exists(@"C:\sample.xml"))
{
File.Delete(@"C:\sample.xml");
xmlDoc.Save(@"C:\sample.xml");
}
else
{
xmlDoc.Save(@"C:\sample.xml");
}
Con.Close();
MessageBox.Show("Completed");
Read XML File and Display in List
XmlReader xReader = XmlReader.Create(@"E:\sample.xml");
while (xReader.Read())
{
switch (xReader.NodeType)
{
case XmlNodeType.Element:
listBox1.Items.Add("<" + xReader.Name + ">");
break;
case XmlNodeType.Text:
listBox1.Items.Add(xReader.Value);
break;
case XmlNodeType.EndElement:
listBox1.Items.Add("</" + xReader.Name + ">");
break;
}
}
No comments:
Post a Comment