Friday 16 December 2011

Convert Xml document to String

    Default.aspx :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server" Mode="Encode"></asp:Literal></div>
</form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<deneme/>");
#region just to remind myself something
XmlNode node = doc.CreateElement("serkan");
doc.DocumentElement.AppendChild(node);
#endregion
StringWriter sw = new StringWriter();
XmlTextWriter tx = new XmlTextWriter(sw);
doc.WriteTo(tx);
// i use literal control because when you set its mode to "encode", you can write xml tags to browser window as they are
Literal1.Text = sw.ToString();

}
}

No comments:

Post a Comment