Home DotNet Writing XML to the page in asp.net effectively
Author:

Some times you would like to write the XML content out into the browser instead of writing it to a file. Here is one way of how to write XML to the page in asp.net effectively.

In this example the ProduceOutput does the actual writing the XML to the browser.

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Text;

//Demo

namespace WebServices

{

    public partial class demo : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {  

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

            MyWebServices.CMM cmm = new CMMWebServices.MyWebServices.CMM();

            XmlNode node = cmm.GetAssignableRole(txtRole.Text);

            ProduceOutput(node); 

        }

 

        private void ProduceOutput(XmlNode node)

        {       

 

            XmlDocument doc = new XmlDocument();           

            doc.LoadXml(node.OuterXml);

            XmlDeclaration xmldecl;

            xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

 

            //Add the new node to the document.

            XmlElement root = doc.DocumentElement;

            doc.InsertBefore(xmldecl, root);

 

            Response.Clear();

            Response.ClearContent();

            Response.ContentType = "text/xml";

 

            doc.Save(Response.OutputStream);

            Response.End();     

        }       

    }

}



Comments (0)
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"