.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Writing XMl Data from DOM

Posted By:Ravi Ranjan Kumar       Posted Date: October 31, 2011    Points: 200    Category: XML    URL: http://www.dotnetspark.com  

This article enable you learn how to Write XMl data from DOM.
 

Introduction 

An xml document can be created by using the XmlDocument class. After a document is created, you can load it with data from a string, a stream a URL, or an XmlReader class by using the Load() method. By using DOM, you can modify an XMl document by creating new nodes. Deleting nodes and so on.

Creating New Nodes in the DOM Tree

New nodes can be created by using the Create method of XmlDocument, where is the type of node. By using the Create method, you can create different type of nodes, such as XML decalartion, element, text nodes and commentds. 
After creating the new nodes, you need to insert the, into the DOM tree. There are various method for performing this task, which have been described in the following table.

Now ues the preceding methods to make modifications to a Dom tree.

Creating an XMl declaration

The XmlDocument class has a CreateXmlDeclaration() method that is used for creating XML declarations. This class has the following paramteres : 

a) Version : The version should be 1.0
b) Encoding : the default encoding of UTF-8 Encoding is used when you save the XMLDocuement to a file or stream. Encoding should be set to a string supported by the Encoding class as otherwise the Save() method will not work. 
c) Standalone : The value is either yes or no. if null is passed the Save() method does not write a standalone attribute in the XML declaration.

The following code snippet create an XML declaration and adds it to a document :
 
     XmlDeclaration XmlDec = doc.CreateXmlDeclaration("1.0", null, null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(XmlDec, root);

Creating Element 

The XmlDocument class has a CreateElement() method that is used for creating XML elements. The following code create an element PRICE and appends it to the XML document:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("" +
                     "Ravi" +
                     "Ranjan Kumar" +                   
                     "");
            XmlElement element = doc.CreateElement("Emp_Designation");
            XmlText text = doc.CreateTextNode("Manager");
            doc.DocumentElement.AppendChild(element);
            doc.DocumentElement.LastChild.AppendChild(text);
            doc.Save(Console.Out);
            Console.Read();  
        }
    }
}

The structure of the XML document after execution of the preceding code is as follows : 


Deleting Nodes from the DOM Tree

You can delete nodes from an XML DOM by using the RemoveChild() method. Consider the following code snippet:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;


namespace DelXml
{
    class Program
    {
        static void Main(string[] args)
        {

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(("" +
                     "" +
                     "Ravi" +
                     "Ranjan Kumar" +
                     "XYZ" +
                     "ABC" +
                     "" +
                     "" +
                     "Vivek" +
                     "Raj" +
                     "XYZ1" +
                     "ABD" +
                     "" +
                     ""));

            XmlNode node = doc.DocumentElement.FirstChild;
            node.RemoveChild(node.FirstChild);
            doc.Save(Console.Out);
            Console.ReadLine();
        }
    }
}


On execution, you will get the following output 

You can download source code for getting more practical influence to know how to accomplish task of XML data writing.  :) 


 Subscribe to Articles

     

Further Readings:

Responses
Author: Naveen         Company URL: http://www.dotnetspark.com
Posted Date: November 24, 2011

thanks
Ravi Ranjan Kumar .as i am new to xml it is very help full to me and help me regarding how to write css and database connectivity through xml.

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend