linq - How to create an XML document

LINQ to XML - How to create and save an XML document
CreateXMLDocument.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Xml.Linq" %>

<!DOCTYPE html>
<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        XDocument xDoc = new XDocument
        (
            new XDeclaration("1.0","utf-8","yes"),
            new XComment("This is an Xml Document which programmatically created by LINQ to XML"),
            new XElement("employees",
                new XElement("employee",
                    new XAttribute("id","101"),
                    new XElement("name","jenny jones"),
                    new XElement("city", "rome")
                             )
                         )
        );

        String xmlFile = Server.MapPath("~/App_Data/Employee.xml");
        xDoc.Save(xmlFile);

        XElement xFile = XElement.Load(xmlFile);
        TextBox1.Text = xFile.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>LINQ to XML - How to create and save an XML document</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkBlue; font-style:italic;">
            LINQ to XML - How to create and save an XML document
        </h2>
        <hr width="600" align="left" color="CornFlowerBlue" />
        <asp:TextBox
             ID="TextBox1" 
             runat="server" 
             TextMode="MultiLine"
             Columns="75"
             Rows="15"
             Enabled="false"
             >
        </asp:TextBox>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Create and Save an XML Document"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More c# linq examples