Wednesday, October 31, 2007

What is XML DOM (Document Object Model)

Why is XML DOM?
XML is a standard by which two incompatible system can communicate. XML DOM works with XML data in your application. It is used to read, write and modify XML document programatically.


Understanding Nodes in XML Document


Node type

Description

Root

This node type is the container for all the nodes and is also known as the document root.

Element

This node type represents element nodes.

Attribute

This node type represents the attributes of an element node.

Text

This node type represents the text that belongs to a particular node or to an attribute.



Example of XML Document, employee.xml
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<employee>
<FirstName>Ajay</FirstName>
<LastName>Thakur</LastName>
<DateOfBirth>08/09/1968</DateOfBirth>
<DateOfJoining>04/01/1992</DateOfJoining>
<Address>2010 Stanley Dr., Charlotte, NJ 08830</Address>
<Basic>2100</Basic>
<Designation>HR Manager</Designation>
<LeaveBalance>12</LeaveBalance>
</employee>
<employee>
<FirstName>Ganesh</FirstName>
<LastName>Patil</LastName>
<DateOfBirth>01/12/1972</DateOfBirth>
<DateOfJoining>06/01/2000</DateOfJoining>
<Address>7862 Freepoint Pkwy, Tampa, NJ 08820</Address>
<Basic>1400</Basic>
<Designation>Software Professional</Designation>
<LeaveBalance>4</LeaveBalance>
</employee>
</employees>


Understanding XML Document
  • Every XML Document contains a single root element that contains all other nodes of the XML Document. Here <employees> is a root element.
  • XML Element is a Record which contains information. Here <employee>
  • XML Element Attribute further describe the Record details. Here <FirstName> etc are XML Element Attributes.

What is XML Document Object?
XMLDocument provides an in-memory representation of and XML document.

Example of XML Document
Create a Asp.net Website
Create a XML Document and name it employee.xml

Loading of XML Document
You can load xml document using XMLDocument object's load method

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDocument EmpXMLDoc = new XmlDocument();
string XMLDocPath = Server.MapPath("Employee.xml");
EmpXMLDoc.Load(XMLDocPath);
Response.Write(EmpXMLDoc.InnerText.ToString());
}
}


Saving XML Document
You can save XML Document with XMLDocument.Save method
Example: XMLDocument.Save("Employee.xml");


Read XML Document and display on WebPage
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
XmlDocument EmpXMLDoc = new XmlDocument();
string XMLDocPath = Server.MapPath("Employee.xml");
EmpXMLDoc.Load(XMLDocPath);

//Read XML Document //Note: Begin for ChildNode[1]
foreach (XmlNode node1 in EmpXMLDoc.ChildNodes[1])
{
foreach (XmlNode node2 in node1.ChildNodes)
{
Response.Write("<b>" + node2.Name + "</b>: " + node2.InnerText + " ");
}
Response.Write("<br>");
}
}
}

//Output

No comments:

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape