Saturday 2 June 2012

Create XML using C#



using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(Server.MapPath("~/employees.xml")))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("Employees");
                DataTable DT = new DataTable();
               
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                   writer.WriteStartElement("Employee");

                   writer.WriteElementString("ID", DT.Rows[i]["ID"].ToString());
                   writer.WriteElementString("FirstName", DT.Rows[i]["Name"].ToString());
                   writer.WriteElementString("Surname", DT.Rows[i]["Surname"].ToString());
                   writer.WriteElementString("Mobile", DT.Rows[i]["Mobile"].ToString());
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }

No comments:

Post a Comment