Load XML from String and write to file : XML File Save « XML « ASP.Net






Load XML from String and write to file

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

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        XmlDocument empDoc = new XmlDocument();
        Response.ContentType = "text/xml";
        try
        {
            //Load the XML from a String
            empDoc.LoadXml("<employees>" +
                        "<employee id='1'>" +    
                        "<name><firstName>First Name</firstName>" + 
                        "<lastName>Last Name</lastName>" +                       
                        "</name><city>City</city>" +
                        "<state>WA</state><zipCode>99999</zipCode>" +
            "</employee></employees>");    
            //Save the XML data onto a file                    
            empDoc.Save(MapPath("EmployeesNew.xml"));
            Response.Write(empDoc.InnerXml);
        }
        catch (XmlException xmlEx)
        {
            Response.Write("XmlException: " + xmlEx.Message);
        }
        catch (Exception ex)
        {
            Response.Write("Exception: " + ex.Message);
        }        
    }   
</script>



           
       








Related examples in the same category

1.Save updated XML data set to new XML file
2.Save XML data to String Writer
3.XML File write: element, comments
4.XML file write: Indent, indent char, and Omit Xml Declaration
5.Write XML File with namespace