Monday, October 27, 2008

Creating and modifying XML Document using XML DOM in ASP.Net

Introduction

In .Net, modifying an XML document is a task that is central to developing XML based application (Either it is a web application or windows application). Microsoft .Net Framework has classes that can be used to perform this task. Mainly the class we used is in the namespace System.XML.

In this article we’ll learn how to manipulating XML Document using XML DOM (XML Document Object Model). The XML DOM provides a programming interface for applications to manipulate XML. This implementation is fully support the W3C standard and provides additional features that make it easier to program applications to work with XML files.

Why using XML DOM? Not using XML Reader?

Main reason for using XML DOM (Document Object Model) rather than using XML Reader is because XML Reader can only read XML Document by forward only and read only. By using XML DOM we can read XML document by non sequential or by random. This can be done because DOM cache XML data in memory, and by doing this, an application can use DOM to:

  1. Search specific content in an XML Document
  2. Add, remove, or replace content (nodes, properties, or it’s value)
  3. Save result to an XML File

Creating XML Document

XMLDocument object acts as a container for an XML document. You typically create an XMLDocument and load it with XML from a file, string, or a stream.



Now we look how to create XML using XmlDocument.

Dim XmlDoc As New XmlDocument

Dim XmlDecl As XmlDeclaration
XmlDecl = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "")
XmlDoc.AppendChild(XmlDecl)
Dim RootElem As XmlElement
RootElem = XmlDoc.CreateElement("Employee")
XmlDoc.AppendChild(RootElem)
RootElem.SetAttribute("Name", "Hendra")
RootElem.SetAttribute("Age", 26)
Dim DeptElem As XmlElement
DeptElem = XmlDoc.CreateElement("Department")
RootElem.AppendChild(DeptElem)
DeptElem.InnerText = "Information Technology Department"
XmlDoc.Save("c:\employee.xml")

With the code above, you will get a XML file with content as below :

"1.0" encoding="UTF-8"?>

"Hendra" Age="26">
Information Technology Department

See, it’s easy to generate a XML file with predefine content by using XML DOM.

Reading XML Document

XMLDocument can be loaded with a XML file, string, or a stream.

Example :

Dim XmlDoc As New XmlDocument

XmlDoc.Load("c:\employee.xml")
Dim employees As XmlNodeList
employees = XmlDoc.SelectNodes("//Employee ")
Dim item As XmlElement
For Each item In employees
Response.write(item.GetAttribute("
Name") )
Next

By using XmlDocument, it’s easy to retrieve nodes of an XML File. Then you can retrieve it’s attribute, values, or inner text / XML.

Modifying XML Document
XML DOM enable you to modify XML Document

Example :

    Dim XmlDoc As New XmlDocument

XmlDoc.Load("C:\employee.xml")
Dim elements As XmlNodeList
elements = XmlDoc.SelectNodes("//Employee")
Dim elemen As XmlElement
For Each elemen In elements
Dim EmployeeName As String
EmployeeName = elemen.GetAttribute("
Name")
'We use xpath to querying Employee element
Dim xpath As String
xpath = "
//Employee[@Name='" & EmployeeName & "']"
Dim EmployeeElement As XmlElement
EmployeeElement = XmlDoc.SelectSingleNode(xpath)
'We now update the age attribute in Employee node / element
Dim x As Integer
x = Int32.Parse(EmployeeElement.GetAttribute("Age"))
x = x + 5
EmployeeElement.SetAttribute("Age", x.ToString)
Next
XmlDoc.Save("C:\employee.xml")

The code above is used to modify a XML file content. In the example as you can see, we use XML DOM to read XML File, then using xpath to querying XML content. And then we use XmlElement in XML DOM to set the attribute of the XmlElement.

No comments: