Click here to Skip to main content
16,004,686 members
Articles / Programming Languages / XML
Article

XML DOM in .NET Framework - Part 1

Rate me:
Please Sign up or sign in to vote.
2.92/5 (32 votes)
27 Jan 2003CPOL3 min read 90.3K   1.1K   16   3
The article explains how to use the XML DOM in .NET Framework.

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.

C#
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.

C#
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.

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

Now we replace the text with the replace method.

C#
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.

C#
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)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralXML DOM Pin
Mesfun12-Apr-06 22:17
Mesfun12-Apr-06 22:17 
Generaldatabase Pin
doggy8211129-Mar-04 22:06
doggy8211129-Mar-04 22:06 
GeneralRe: database [modified] Pin
Zeeshan Anwar30-Mar-04 18:16
Zeeshan Anwar30-Mar-04 18:16 
Hi,
You can do that by having a query which will return you the data in Xml format and that you can load that Xml in the XmlDom like.
<br />
select * from MyTable FOR XML AUTO,ELEMENTS<br />

This query will return you that data in Xml format like
<br />
<MyTable><id>1</id><Name>Zeeshan</Name><Address>abc</Address></MyTable><br />


and now you can load that Xml in the DOM by using xmldoc.LoadXml(returnQueryResult) method.
Hopefully that will solve your problem.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.