WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 88 users


WorldofASP.NET >> ASP.NET >> General ASP.NET

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here

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

Creating and modifying XML Document using XML DOM in ASP.Net
Published Date : 25 Mar 2008
Author : Hendra
Language : VB.NET
Platform : .NET
Technology : ASP.NET
Views : 11316
Rating : (0 votes so far)


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 :

<?xml version="1.0" encoding="UTF-8"?> 
<Employee Name="Hendra" Age="26"> 
<Department>Information Technology Department</Department> 
</Employee>

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='" &amp; EmployeeName &amp; "']" 
        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.



Other Related and Popular Articles :

Working with Master Pages and Basics of Master Pages
Why do you need master pages and how it can improve the maintenance of your website

Tips to Improve Your ASP.NET Web site performance
The article contains guidelines for improving your ASP.NET Web applications

Use C# and VB.NET in the same project
Use C# and VB.NET classes in the same ASP.NET application

Publish and create RSS Feeds easily on your ASP.NET websites
This article explains the concept of creating RSS feeds for your ASP.NET website with auto updating contents

Working with HttpWebRequest and HttpWebResponse in ASP.NET
This article explain how to use WebRequest and WebResponse in ASP.NET to grab contents and screen scrape

Sending Email in ASP.NET 1
This article explain how to send email in ASP.NET 1 either by using SMTP authentication or not


Author Profile : Hendra

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

Leave New Comments


Article Content copyright by Hendra
Everything else Copyright © by WorldofASP.NET 2008





Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card