Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Creating new XML Document in ASP.NET Programmatically
 

Creating new XML Document in ASP.NET Programmatically

by Faisal Khan.Follow Faisal Khan on Twitter

Introduction
In this tutorial, we will learn how to create an XML Document programmatically. We will make use of XmlDocument class from System.Xml namespace. Following topics will be covered in this tutorial:

  • How to create XML declaration and element nodes.
  • How to create attributes and append them to element nodes.
  • How to create CDATA sections within element nodes.
  • How to nest elements and attributes in one another to create a logical tree like structure.
  • How to send the dynamically generated XML document to the client browser with proper headers.

This tutorial is 4th one in the series of tutorials about XML and ASP.NET:

Creating new XML Document
We will now write down the code to create new XML document.

Code for ASP.NET page
Copy and paste following code into a new text file and then save it as "CreateXmlDocument.aspx" in your ASP.NET web application:

<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/xml" %>

<%@ Import Namespace="System.Xml" %>

<script runat="server">
  protected void Page_Load(object source, EventArgs e)
  {
    XmlDocument doc = new XmlDocument();

    // XML declaration
    XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
    doc.AppendChild(declaration);

    // Root element: article
    XmlElement root = doc.CreateElement("article");
    doc.AppendChild(root);

    // Sub-element: author
    XmlElement author = doc.CreateElement("author");
    author.InnerText = "Faisal Khan";
    root.AppendChild(author);

    // Attribute: isadmin
    XmlAttribute isadmin = doc.CreateAttribute("isadmin");
    isadmin.Value = "true";
    author.Attributes.Append(isadmin);
    
    // Sub-element: title
    XmlElement title = doc.CreateElement("title");
    title.InnerText = "Sample XML Document";
    root.AppendChild(title);

    // Sub-element: body (CDATA)
    XmlElement body = doc.CreateElement("body");
    XmlNode cdata = doc.CreateCDataSection("This is the body of the article.");
    body.AppendChild(cdata);
    root.AppendChild(body);
    
    doc.Save(Response.OutputStream);
  }
</script>

Explanation
The first thing we do in our ASP.NET page is to declare, that we will use "C#" as the programming language and that, the content-type of this ASP.NET page will be "text/xml".

Note: The content-type of an XML document should be "text/xml" for it to be properly handled by the client browser.

<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/xml" %>

Next, we import "System.Xml" namespace in our ASP.NET page. The XmlDocument class we will use later to generate an XML document dynamically, is present in this namespace.

<%@ Import Namespace="System.Xml" %>

A 'script' tag whose 'runat' attribute is set to "server" is required for ASP.NET to process this code on the server-side.

<script runat="server">
	...
</script>

We are going to handle the Load event of the ASP.NET page and place the code to generate XML document dynamically, in the Page_Load() method.

protected void Page_Load(object source, EventArgs e)
{
	...
}

First, we create a new instance of XmlDocument class.

XmlDocument doc = new XmlDocument();

Every XML document has a declaration statement as the first line of the document, enclosed in <?xml and ?> tags and having an attribute with the name of 'version', whose value it set to "1.0":

<?xml version="1.0" ?>

To create this declaration, we call the CreateNode() method of XmlDocument class. The first argument to this method is the type of node you want to create. We pass XmlNodeType.XmlDeclaration as the type because we want to create an XML declaration node. Once that node is created, we pass its reference to the AppendChild() method of XmlDocument instance we created earlier to append this node in the XML document we are generating.

Note: If you do not append the newly created declaration node in the XmlDocument instance, it will not appear in the XML document.

// XML declaration
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.AppendChild(declaration);

Next, we create the root element of our XML document. A root element is the element in which all other elements are nested i.e., are sub-elements of the root element.

To create the root element, we call the CreateElement() method of XmlDocument class and as its argument, we pass the name of the root element, "article". And like before, we append this element to the XmlDocument instance.

Note: The name of the root element can be anything. For the purpose of this tutorial, we are naming it "article".

// Root element: article
XmlElement root = doc.CreateElement("article");
doc.AppendChild(root);

The XML document at this moment, if generated, will look like this:

<?xml version="1.0"?>
<article />

Next, we create a sub-element and nest it in the root element. To do that, we call the CreateElement() method of XmlDocument instance, and as its argument, we pass the name of this element, "author". Then we set the inner text of that element by setting the InnerText property of newly created XmlElement object. Finally, as before to make this element appear in the XML document, we append this element to the root element.

// Sub-element: author
XmlElement author = doc.CreateElement("author");
author.InnerText = "Faisal Khan";
root.AppendChild(author);

At this moment, if generated, the XML document will look like this:

<?xml version="1.0"?>
<article>
	<author>Faisal Khan</author>
</article>

Introduction
In this tutorial, we will learn how to create an XML Document programmatically. We will make use of XmlDocument class from System.Xml namespace. Following topics will be covered in this tutorial:

  • How to create XML declaration and element nodes.
  • How to create attributes and append them to element nodes.
  • How to create CDATA sections within element nodes.
  • How to nest elements and attributes in one another to create a logical tree like structure.
  • How to send the dynamically generated XML document to the client browser with proper headers.

This tutorial is 4th one in the series of tutorials about XML and ASP.NET:

Creating new XML Document
We will now write down the code to create new XML document.

Code for ASP.NET page
Copy and paste following code into a new text file and then save it as "CreateXmlDocument.aspx" in your ASP.NET web application:

<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/xml" %>

<%@ Import Namespace="System.Xml" %>

<script runat="server">
  protected void Page_Load(object source, EventArgs e)
  {
    XmlDocument doc = new XmlDocument();

    // XML declaration
    XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
    doc.AppendChild(declaration);

    // Root element: article
    XmlElement root = doc.CreateElement("article");
    doc.AppendChild(root);

    // Sub-element: author
    XmlElement author = doc.CreateElement("author");
    author.InnerText = "Faisal Khan";
    root.AppendChild(author);

    // Attribute: isadmin
    XmlAttribute isadmin = doc.CreateAttribute("isadmin");
    isadmin.Value = "true";
    author.Attributes.Append(isadmin);
    
    // Sub-element: title
    XmlElement title = doc.CreateElement("title");
    title.InnerText = "Sample XML Document";
    root.AppendChild(title);

    // Sub-element: body (CDATA)
    XmlElement body = doc.CreateElement("body");
    XmlNode cdata = doc.CreateCDataSection("This is the body of the article.");
    body.AppendChild(cdata);
    root.AppendChild(body);
    
    doc.Save(Response.OutputStream);
  }
</script>

Explanation
The first thing we do in our ASP.NET page is to declare, that we will use "C#" as the programming language and that, the content-type of this ASP.NET page will be "text/xml".

Note: The content-type of an XML document should be "text/xml" for it to be properly handled by the client browser.

<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/xml" %>

Next, we import "System.Xml" namespace in our ASP.NET page. The XmlDocument class we will use later to generate an XML document dynamically, is present in this namespace.

<%@ Import Namespace="System.Xml" %>

A 'script' tag whose 'runat' attribute is set to "server" is required for ASP.NET to process this code on the server-side.

<script runat="server">
	...
</script>

We are going to handle the Load event of the ASP.NET page and place the code to generate XML document dynamically, in the Page_Load() method.

protected void Page_Load(object source, EventArgs e)
{
	...
}

First, we create a new instance of XmlDocument class.

XmlDocument doc = new XmlDocument();

Every XML document has a declaration statement as the first line of the document, enclosed in <?xml and ?> tags and having an attribute with the name of 'version', whose value it set to "1.0":

<?xml version="1.0" ?>

To create this declaration, we call the CreateNode() method of XmlDocument class. The first argument to this method is the type of node you want to create. We pass XmlNodeType.XmlDeclaration as the type because we want to create an XML declaration node. Once that node is created, we pass its reference to the AppendChild() method of XmlDocument instance we created earlier to append this node in the XML document we are generating.

Note: If you do not append the newly created declaration node in the XmlDocument instance, it will not appear in the XML document.

// XML declaration
XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
doc.AppendChild(declaration);

Next, we create the root element of our XML document. A root element is the element in which all other elements are nested i.e., are sub-elements of the root element.

To create the root element, we call the CreateElement() method of XmlDocument class and as its argument, we pass the name of the root element, "article". And like before, we append this element to the XmlDocument instance.

Note: The name of the root element can be anything. For the purpose of this tutorial, we are naming it "article".

// Root element: article
XmlElement root = doc.CreateElement("article");
doc.AppendChild(root);

The XML document at this moment, if generated, will look like this:

<?xml version="1.0"?>
<article />

Next, we create a sub-element and nest it in the root element. To do that, we call the CreateElement() method of XmlDocument instance, and as its argument, we pass the name of this element, "author". Then we set the inner text of that element by setting the InnerText property of newly created XmlElement object. Finally, as before to make this element appear in the XML document, we append this element to the root element.

// Sub-element: author
XmlElement author = doc.CreateElement("author");
author.InnerText = "Faisal Khan";
root.AppendChild(author);

At this moment, if generated, the XML document will look like this:

<?xml version="1.0"?>
<article>
	<author>Faisal Khan</author>
</article>

 ( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions

No Comments Found.


Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.