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.