Search This Blog

Tuesday, July 2, 2013

Create XML file on local system

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
//***Add below class for XElement
using System.Xml;
using System.Text;
using System.Xml.Linq;
/*LINQ—language integrated query—introduces many extensions methods to the standard C# environment. 
  These methods work on Lists, arrays and collections that are not yet in memory. 
*/

public partial class Linq : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        create_xml();
   
    }

   private void create_xml()
    {
        XNamespace empNM = "urn:lst-hotel:hotel";

        XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-16", null),
                    new XElement(empNM + "HotelRoomAvailability",
                        new XElement("Hotel",
                            new XComment("Hotel element in below:"),
                            new XElement("Code", "5"),
                            new XElement("Name", "Le Marly"),
                            new XElement("Address", "Hamra, Main Street, Beirut, Lebanon.")
                            )));

        StringWriter sw = new StringWriter();
        XmlWriter xWrite = XmlWriter.Create(sw);
        xDoc.Save(xWrite);
        xWrite.Close();

        // Save to Disk
        xDoc.Save(Server.MapPath("~/xml/hotel1.xml"));
        Response.Write("XML save in disk");
    }
}
OUTPUT:
XML save in disk 

No comments :