ASP.NET - Load XmlDocument from string variable and save file

asp.net and XmlDocument - Load XmlDocument from string variable and save file
XmlDocumentLoadString.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Xml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    void Button1_Click(object sender, System.EventArgs e)
    {
        string xmlPath = Request.PhysicalApplicationPath + @"App_Data\books.xml";
        XmlDocument xmlDoc = new XmlDocument();
        try
        {
            string rawXml = "<books>" +
                          "<book>" +
                            "<name>Beginning XML Databases</name>" +
                            "<author>Gavin Powell</author>" +
                          "</book>" +
                          "<book>" +
                            "<name>ASP.NET 3.5 Website Programming: Problem - Design - Solution</name>" +
                            "<author>Chris Love</author>" +
                          "</book>" +
                        "</books>";
            xmlDoc.LoadXml(rawXml);
            xmlDoc.Save(xmlPath);
            Label1.Text = "XmlDocument loading successful !";
        }
        catch (XmlException xmlEx)
        {
            Response.Write("Xml Exception: " + xmlEx.Message.ToString());
        }
        catch (Exception ex)
        {
            Response.Write("Error occured: " + ex.Message.ToString());
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>asp.net and XmlDocument - Load XmlDocument from string variable and save file</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Green; font-style:italic;">XmlDocument Example: Load XmlDocument From String</h2>
        <asp:Label 
            ID="Label1" 
            runat="server"
            Font-Bold="true"
            Text=""
            ForeColor="DeepSkyBlue"
            >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Load XmlDocument From String"
            Height="42"
            Font-Bold="true"
            ForeColor="Crimson"
            />
    </div>
    </form>
</body>
</html>








Related asp.net example