5,666,979 members and growing! (16,439 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Beginner License: The Code Project Open License (CPOL)

XML DOM in .NET Framework - Part 1

By Zeeshan Anwar

The article explains how to use the XML DOM in .NET Framework.
C#, XML, Windows, .NET 1.0, .NETVisual Studio, VS.NET2002, Dev

Posted: 27 Jan 2003
Updated: 27 Jan 2003
Views: 59,089
Bookmarked: 10 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
32 votes for this Article.
Popularity: 4.20 Rating: 2.79 out of 5
10 votes, 31.3%
1
7 votes, 21.9%
2
1 vote, 3.1%
3
4 votes, 12.5%
4
10 votes, 31.3%
5

Introduction

I have searched over the net to find some material about the XML DOM but could not find that, so eventually I decided to write one, which will help the other developers to start working in XML DOM. Believe me, it is very easy, but the problem is where to start it. This article gives you some information about the XML DOM. The is the 1st part of the XML DOM implementation in .NET. It includes XmlDocument, XmlElement, XmlAttribute, XmlNode, XmlNodeList. The 2nd Part will cover XPath, XslTransform and XML and ADO.NET. So before starting this series I assume that you are familiar with the Xml.

The XML DOM is implemented through the XmlNode class. The XmlNode is an abstract class that represents an XML document. XmlNodeList is an ordered list of nodes. The two classes are the core of the XML DOM implementation in .NET. The classes that we are dealing here are XmlDatadocument, XmlDocument, XmlNode, XmlNodeList, XmlElement and XmlAttribute. These classes and more are found under the namespace System.Xml. Let’s start with the sample application. Open the main menu stated as XmlDom, under that you will find some child menus. Let's discuss them one by one. The first menu labeled as LoadXml, it shows us how to use that function.

xmldoc.LoadXml("" + "<title>Pride And Prejudice</title>" + ""); 

Note that we have passed the tags in the function parameter instead of the file name. If we want to pass the file name then this function will throw an exception. This method does not validate the XML file with the XML schema or DTD.

XmlElement element = xmldoc.CreateElement("title"); 

In the above line, we made a new object of the XmlElement and call the XmlDocument method createElement and in the parameter of the method, we add the element name. The method createElement is overloaded so we can also use the rest of the two methods of these. Now we want to add the text of the element/tag. For that we call a function InnerText to set the text of the tag.

element.InnerText = "How to load Xml file with XmlDom"; 

Now we replace the text with the replace method.

root.ReplaceChild(element,root.FirstChild); 

In the next menu, we load the XML file with the load method. It reads the XML file stream from the disk. Next we want to create an element in that file but this time we want that we insert that element under the tag. For that we create an element and set its text and then search the tag.

XmlNodeList node = xmldoc.GetElementsByTagName("system.web"); 

Through that we now have the reference of the tag. Now add the new element under that tag through the method AppendChild. For creating the attribute of any element, XML DOM provides the class XmlAttribute. We achieve that the same way as we did with creating element. The only difference is that the element has InnerText property and attribute has the Value property instead of that. There is another way by which we can set the attribute of any element, that is through the method SetAttribute of XmlElement class. It takes two parameters, the name of the attribute and the value of the attribute.

That’s all folks. Do log back again to check the next article in series.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zeeshan Anwar



Occupation: Architect
Location: Pakistan Pakistan

Other popular C / C++ Language articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralXML DOMmemberMesfun23:17 12 Apr '06  
Generaldatabasememberdoggy8211123:06 29 Mar '04  
GeneralRe: database [modified]memberZeeshan Anwar19:16 30 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Jan 2003
Editor: Smitha Vijayan
Copyright 2003 by Zeeshan Anwar
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project