Load XML from string and output to browser : XML Output « XML « ASP.Net






Load XML from string and output to browser

<%@ 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.Read XML file and output to client
2.Use XmlDocument (DOM) to load xml file and output to browser
3.Read and format XML data into HTML