Пример #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Location of CSV file.
            String csvFile = Server.MapPath("employees.csv");

            // Load file into a stream reader.
            StreamReader reader = new StreamReader(csvFile);

            // Parse the CSV file into an array list.
            ArrayList employees = HList.ToCSVList(reader);

            // Initialize a memory to output xml at the end.
            MemoryStream ms = new MemoryStream();

            using (XmlTextWriter xml = new XmlTextWriter(ms, Encoding.UTF8))
            {
                // Root.
                xml.WriteStartDocument();
                xml.WriteStartElement("employees");

                // Process each employee.
                for (int i = 1; i < employees.Count; i++)
                {
                    List <String> li = employees[i] as List <String>;
                    this.WriteEmployeeData(xml, li);
                }

                // End.
                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                ms.Position = 0;
            }

            // Set content type and header for output.
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.AddHeader("Content-Disposition", "attachment;filename=employees.xml");
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }