.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

Reading XML Data into DOM

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

This article enable you to learn how to processing data for reading purpose into DOM.
 

Introduction 

Many application need the ability to read an entire XML document into memory, to access XML content in any order, and to modify the XML content in memory. The XML Document Object Model(DOM) is an Application Programming Interface(API) that provides full read-write random access capabilities for reading and writing XML data.
DOM is a standard way of representing an XML document as an internal structure. You can write code to access the various element, attribute and text within an XML document by using various properties and method of the XML DOM API. While the XmlReader and XmlWriter  classes give you serial access to an XML document. The DOM API gives you random access to any element or attribute in the XML document. 
The .Net Framework provides full support for the DOM API

Reading XML Data into DOM 

To read data in XML by using DOM, you can retrieve sections of the documents by retrieving nodes in the document. You can retrieve a sey of nodes and put it in an orderd or unordered collection. When the nodes are put into an unordered collection, the set is called a NameNodeMap by the W3C. you can retrieve the data by name or index in this type of collection. Putting a set of nodes in an ordered colletion is called a NodeList by XmlNamedNodeMap class, and NoceList is implemented by the XmlNodeList class.

Consider the example of XYZ Inc. the different employee details are stored in an EmpDetails.Xml file. The Payroll head of this company want the information about the number of Employee and the details of each and every employee.
The preceding task can be easily done by loading the XML file into a DOM tree..
You can load an XML document by using the XmlDocument class. The XmlDocument class provides the following into methods to load an XML document into memory:

Load() :  This method loads XML data from a stream, a string an XmlReader or a TextReader. The following code loads an XML document from a TextReader object: 

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

namespace loadXMLTextReader
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            // Load the document 
            XmlTextReader reader = new XmlTextReader(@"F:\EmpDetails.XML");
            reader.Read();
            // load the xml document from the specified xmlReader
            doc.Load(reader); 
            doc.Save(Console.Out); // 
            Console.ReadLine();
        }
    }
}

When you execute the preceding code, you wull get the following output  


LoadXml() :  This method is used load an XMl file from a string. The following code loads an XML document by using the LoadXml() method:

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

namespace loadXmlString
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("" +
                     "" +
                     "Ravi" +
                     "Ranjan Kumar" +
                     "XYZ" +
                     "ABC" +
                     "" +
                    
                     "" +
                     "Vivek" +
                     "Raj" +
                     "XYZ1" +
                     "ABD" +
                     ""+
                     "");

            //Display the XML file.
            doc.Save(Console.Out);
            Console.ReadLine();
                        
        }
    }
}

The preceeding code gives the following output on execution.

You can also save the preceding XML data into a disk file called EmpDetails.xml in your drive by given line of code.

doc.Save(@"C:\EmpDetails.xml");
To get practical influence you can download attached source code.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

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